View Javadoc

1   package org.kuali.ole.docstore.discovery.service;
2   
3   import org.apache.solr.client.solrj.SolrQuery;
4   import org.apache.solr.client.solrj.SolrServer;
5   import org.apache.solr.client.solrj.response.QueryResponse;
6   import org.apache.solr.common.SolrDocument;
7   import org.apache.solr.common.SolrDocumentList;
8   import org.kuali.ole.docstore.model.bo.OleDocument;
9   import org.kuali.ole.docstore.model.bo.WorkBibDocument;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  import java.util.Map;
14  import java.util.Set;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: ?
19   * Date: 8/6/12
20   * Time: 11:42 AM
21   * To change this template use File | Settings | File Templates.
22   */
23  public class SRUCQLQueryServiceImpl implements SRUCQLQueryService{
24  
25     private static SRUCQLQueryServiceImpl srucqlQueryService=null;
26  
27      private SRUCQLQueryServiceImpl(){
28      }
29  
30      public static SRUCQLQueryServiceImpl getInstance(){
31          if(srucqlQueryService==null)
32              srucqlQueryService=new SRUCQLQueryServiceImpl();
33          return srucqlQueryService;
34      }
35  
36      /**
37       *
38       * @param solrQuery
39       * @return   return list of bib id values
40       * @throws Exception
41       */
42      @Override
43      public List<OleDocument> queryForBibDocs(Map reqParamMap,String solrQuery) throws Exception{
44          List<OleDocument> oleDocuments;
45          List<Map<String, Object>> solrHits;
46          solrQuery=getSolrReqQuery(reqParamMap,solrQuery);
47          solrHits =getSolrHitsForQuery(solrQuery,reqParamMap);
48          oleDocuments =buildOleDocuments(solrHits);
49          return oleDocuments;
50  
51      }
52  
53  
54      /**
55       *
56       * @param term
57       * @param relation
58       * @param index
59       * @param solrQueryFlag
60       * @return    solr query
61       * @throws Exception
62       */
63      @Override
64      public String getQuery(String term,String relation,String index,boolean solrQueryFlag)throws Exception{
65  
66          if(term!=null){
67              if(term.contains(":"))   {
68                  solrQueryFlag=false;
69                  return null;
70              }
71          }
72  
73          if(index.contains("dc.")){
74              index=index.replaceAll("dc.","");
75          }
76  
77          if(SRU_QUERY_SRC_RELATION.equals(relation)){
78              return "(all_text:("+term+"))";
79          }
80          if(SRU_QUERY_ALL_RELATION.equals(relation)) {
81              StringBuffer query=new StringBuffer("");
82              String[] terms=term.split(" ");
83              for(int i=0;i<terms.length-1;i++)
84                  query.append(getIndex(index)+"("+terms[i]+")) AND ");
85              query.append(getIndex(index)+"("+terms[terms.length-1]+"))");
86              return query.toString();
87          }
88          if(SRU_QUERY_EXACT_RELATION.equals(relation)){
89              return getExactIndex(index)+"("+term+"))";
90          }
91          if(SRU_QUERY_ANY_RELATION.equals(relation)) {
92              StringBuffer query=new StringBuffer("");
93              String[] terms=term.split(" ");
94              for(int i=0;i<terms.length-1;i++)
95                  query.append(getIndex(index)+"("+terms[i]+")) OR ");
96              query.append(getIndex(index)+"("+terms[terms.length-1]+"))");
97              return query.toString();
98          }
99          if(relation.contains(SRU_QUERY_LESSERTHAN_RELATION)) {
100             StringBuffer query=new StringBuffer("");
101             if(SRU_QUERY_LESSERTHAN_EQUAL_RELATION.equals(relation))
102                query.append(getIndex(index)+"[* TO "+term+"])");
103              else{
104                 int pubDate= Integer.parseInt(term)-1;
105                 query.append(getIndex(index)+"[* TO "+pubDate+"])");
106             }
107             return query.toString();
108         }
109 
110         if(relation.contains(SRU_QUERY_GREATERTHAN_RELATION)) {
111             StringBuffer query=new StringBuffer("");
112             if(SRU_QUERY_GREATERTHAN_EQUAL_RELATION.equals(relation))
113                 query.append(getIndex(index)+"["+term+" TO *])");
114             else{
115                 int pubDate= Integer.parseInt(term)-1;
116                 query.append(getIndex(index)+"["+pubDate+" TO *])");
117             }
118             return query.toString();
119         }
120 
121         if(SRU_QUERY_TITLE.equalsIgnoreCase(index))
122             return getIndex(index)+"("+term+"))";
123         if(SRU_QUERY_AUTHOR.equalsIgnoreCase(index))
124             return  getIndex(index)+"("+term+"))";
125         if(SRU_QUERY_PUBLICATION_DATE.equalsIgnoreCase(index))
126             return  getIndex(index)+"("+term+"))";
127         if(SRU_QUERY_ISBN.equalsIgnoreCase(index))
128             return  getIndex(index)+"("+term+"))";
129         if(SRU_QUERY_PUBLISHER.equalsIgnoreCase(index))
130             return  getIndex(index)+"("+term+"))";
131 
132         return "";
133 
134     }
135 
136     /**
137      *
138      * @param index
139      * @return   query fields string
140      */
141     public String getIndex(String index){
142 
143         if(SRU_QUERY_TITLE.equalsIgnoreCase(index))
144             return "(Title_search:";
145         if(SRU_QUERY_AUTHOR.equalsIgnoreCase(index))
146             return  "(Author_search:";
147         if(SRU_QUERY_PUBLICATION_DATE.equalsIgnoreCase(index))
148             return  "(PublicationDate_search:";
149         if(SRU_QUERY_ISBN.equalsIgnoreCase(index))
150             return  "(ISBN_search:";
151         if(SRU_QUERY_PUBLISHER.equalsIgnoreCase(index))
152             return  "(Publisher_search:";
153 
154         return "";
155     }
156 
157     /**
158      *
159      * @param index
160      * @return   query fields string
161      */
162     public String getExactIndex(String index){
163 
164         if(SRU_QUERY_TITLE.equalsIgnoreCase(index))
165             return "(Title_display:";
166         if(SRU_QUERY_AUTHOR.equalsIgnoreCase(index))
167             return  "(Author_display:";
168         if(SRU_QUERY_PUBLICATION_DATE.equalsIgnoreCase(index))
169             return  "(PublicationDate_display:";
170         if(SRU_QUERY_ISBN.equalsIgnoreCase(index))
171             return  "(ISBN_display:";
172         if(SRU_QUERY_PUBLISHER.equalsIgnoreCase(index))
173             return  "(Publisher_display:";
174 
175         return "";
176     }
177 
178     /**
179      *
180      * @param sortKey
181      * @return
182      */
183     public String getSortKeyValue(String sortKey){
184 
185         String sortKeys[]=sortKey.split(",");
186         if(SRU_QUERY_TITLE.equalsIgnoreCase(sortKeys[0]))
187             return "Title_sort asc";
188         if(SRU_QUERY_AUTHOR.equalsIgnoreCase(sortKeys[0]))
189             return  "Author_sort asc";
190         if(SRU_QUERY_PUBLICATION_DATE.equalsIgnoreCase(sortKeys[0]))
191             return  "PublicationDate_sort asc";
192         if(SRU_QUERY_ISBN.equalsIgnoreCase(sortKeys[0]))
193             return  "ISBN_sort asc";
194         if(SRU_QUERY_PUBLISHER.equalsIgnoreCase(sortKeys[0]))
195             return  "Publisher_sort asc";
196 
197         return "";
198     }
199 
200     /**
201      * @param hitsOnPage
202      * @return List of OleDocuments
203      */
204     public List<OleDocument> buildOleDocuments(List<Map<String, Object>> hitsOnPage) {
205         List<OleDocument> oleDocuments = new ArrayList<OleDocument>();
206         for (Map<String, Object> hitsOnPageItr : hitsOnPage) {
207             WorkBibDocument workBibDocument = new WorkBibDocument();
208             Map map = hitsOnPageItr;
209             Set keys = map.keySet();
210             for (Object key : keys) {
211                 if ("id".equalsIgnoreCase(key.toString())) {
212                     workBibDocument.setId((String)map.get(key));
213                 }
214 
215             }
216             oleDocuments.add(workBibDocument);
217         }
218         return oleDocuments;
219     }
220 
221     /**
222      * @param inputQuery
223      * @return hitsOnPage
224      * @throws Exception Usage: Gets Solr response for input query and builds List of Maps holding Solr Doc Data
225      */
226     public List<Map<String, Object>> getSolrHitsForQuery(String inputQuery,Map reqParamMap) throws Exception {
227         SolrServer server;
228         List<Map<String, Object>> hitsOnPage = new ArrayList<Map<String, Object>>();
229         server = SolrServerManager.getInstance().getSolrServer();
230         SolrQuery solrQuery = new SolrQuery();
231         solrQuery.setQuery(inputQuery);
232         solrQuery.setIncludeScore(true);
233         solrQuery.set("fl", "id");
234         solrQuery.set("start",reqParamMap.get(START_RECORD).toString());
235         solrQuery.set("rows",reqParamMap.get(MAXIMUM_RECORDS).toString());
236         solrQuery.set("sort",getSortKeyValue(reqParamMap.get(SORTKEYS).toString()));
237         QueryResponse queryResponse = server.query(solrQuery);
238         SolrDocumentList solrDocumentList = queryResponse.getResults();
239 
240         for (SolrDocument solrDocument : solrDocumentList) {
241             hitsOnPage.add(solrDocument);
242         }
243         return hitsOnPage;
244     }
245 
246     public String getSolrReqQuery(Map reqParamMap,String solrQuery){
247         StringBuffer query=new StringBuffer("");
248         if(reqParamMap.get(RECORD_SCHEMA)!=null){
249             query.append("(DocFormat:("+reqParamMap.get(RECORD_SCHEMA).toString()+")) AND ");
250         }
251         query.append(solrQuery);
252 
253         return query.toString();
254     }
255 
256 }