View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.facade.search;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Util.ftErrors;
24  
25  import com.fatwire.cs.core.search.data.ResultRow;
26  import com.fatwire.cs.core.search.engine.SearchEngine;
27  import com.fatwire.cs.core.search.engine.SearchEngineConfig;
28  import com.fatwire.cs.core.search.engine.SearchEngineException;
29  import com.fatwire.cs.core.search.engine.SearchResult;
30  import com.fatwire.cs.core.search.query.Operation;
31  import com.fatwire.cs.core.search.query.QueryExpression;
32  import com.fatwire.gst.foundation.CSRuntimeException;
33  import com.fatwire.gst.foundation.facade.ics.ICSFactory;
34  import com.fatwire.search.util.SearchUtils;
35  
36  /**
37   * Simplified SearchEngine class optimized for common use cases
38   *
39   * @author Tony Field
40   * @since Feb 16, 2011
41   */
42  public class SimpleSearchEngine {
43  
44      private final SearchEngine searchEngine;
45      private final SearchEngineConfig seConfig;
46  
47      public SimpleSearchEngine(String engineName) {
48          try {
49              seConfig = SearchUtils.getSearchEngineConfig();
50          } catch (SearchEngineException e) {
51              throw new CSRuntimeException("Could not get search engine config", ftErrors.exceptionerr, e);
52          }
53          searchEngine = seConfig.getEngine(engineName);
54      }
55  
56      /**
57       * Return an instance of the search engine. This is typically done by passing "lucene" as the engineName
58       * parameter because that is the default search engine that ships with Content Server 7.5.
59       *
60       * @param engineName search engine name as configured in Content Server.  Typically set to "lucene".
61       * @return SimpleSearchEngine instance
62       */
63      public static SimpleSearchEngine getInstance(String engineName) {
64          ICS ics = ICSFactory.getOrCreateICS();
65          SimpleSearchEngine sse = (SimpleSearchEngine) ics.GetObj(SimpleSearchEngine.class.getName());
66          if (sse == null) {
67              sse = new SimpleSearchEngine(engineName);
68              ics.SetObj(SimpleSearchEngine.class.getName(), sse);
69          }
70          return sse;
71      }
72  
73      public SearchResultIterable search(QueryExpression query, List<String> indexNames) {
74          SearchResult<ResultRow> sr;
75          try {
76              sr = searchEngine.search(indexNames, query);
77          } catch (SearchEngineException e) {
78              throw new CSRuntimeException("Search failed with an exception. Index: " + indexNames + ", Query: " + query, ftErrors.exceptionerr, e);
79          }
80          return new SearchResultIterable(sr);
81      }
82  
83      /**
84       * @param query
85       * @param indexNames
86       * @return search results based on query and indexes provided.
87       */
88      public SearchResultIterable search(QueryExpression query, String... indexNames) {
89          return search(query, Arrays.asList(indexNames));
90      }
91  
92  
93      /**
94       * @param stringValue
95       * @return a new query based on the passed in string.
96       * @see SearchUtils#newQuery(String)
97       */
98      public QueryExpression newQuery(String stringValue) {
99          return SearchUtils.newQuery(stringValue);
100     }
101 
102     /**
103      * @param fieldName
104      * @param op
105      * @param values
106      * @return a query based on the fields, operation and values.
107      */
108     public QueryExpression newQuery(String fieldName, Operation op, Object... values) {
109         return SearchUtils.newQuery(fieldName, op, Arrays.asList(values));
110     }
111 
112 }