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 tools.gsf.facade.search;
18  
19  import COM.FutureTense.Util.ftErrors;
20  import com.fatwire.cs.core.search.data.ResultRow;
21  import com.fatwire.cs.core.search.engine.SearchEngine;
22  import com.fatwire.cs.core.search.engine.SearchEngineConfig;
23  import com.fatwire.cs.core.search.engine.SearchEngineException;
24  import com.fatwire.cs.core.search.engine.SearchResult;
25  import com.fatwire.cs.core.search.query.Operation;
26  import com.fatwire.cs.core.search.query.QueryExpression;
27  import com.fatwire.search.util.SearchUtils;
28  import tools.gsf.runtime.CSRuntimeException;
29  
30  import java.util.Arrays;
31  import java.util.List;
32  
33  /**
34   * Simplified SearchEngine class optimized for common use cases
35   *
36   * @author Tony Field
37   * @since Feb 16, 2011
38   */
39  public class SimpleSearchEngine {
40  
41      private final SearchEngine searchEngine;
42      private final SearchEngineConfig seConfig;
43  
44      public SimpleSearchEngine(String engineName) {
45          try {
46              seConfig = SearchUtils.getSearchEngineConfig();
47          } catch (SearchEngineException e) {
48              throw new CSRuntimeException("Could not get search engine config", ftErrors.exceptionerr, e);
49          }
50          searchEngine = seConfig.getEngine(engineName);
51      }
52  
53      /**
54       * Return an instance of the search engine. This is typically done by passing "lucene" as the engineName
55       * parameter because that is the default search engine that ships with Content Server 7.5.
56       *
57       * @param engineName search engine name as configured in Content Server.  Typically set to "lucene".
58       * @return SimpleSearchEngine instance
59       */
60      public static SimpleSearchEngine getInstance(String engineName) {
61          return new SimpleSearchEngine(engineName);
62      }
63  
64      public SearchResultIterable search(QueryExpression query, List<String> indexNames) {
65          SearchResult<ResultRow> sr;
66          try {
67              sr = searchEngine.search(indexNames, query);
68          } catch (SearchEngineException e) {
69              throw new CSRuntimeException("Search failed with an exception. Index: " + indexNames + ", Query: " + query, ftErrors.exceptionerr, e);
70          }
71          return new SearchResultIterable(sr);
72      }
73  
74      /**
75       * @param query      query expression object
76       * @param indexNames array of index names
77       * @return search results based on query and indexes provided.
78       */
79      public SearchResultIterable search(QueryExpression query, String... indexNames) {
80          return search(query, Arrays.asList(indexNames));
81      }
82  
83  
84      /**
85       * @param stringValue string value for new query
86       * @return a new query based on the passed in string.
87       * @see SearchUtils#newQuery(String)
88       */
89      public QueryExpression newQuery(String stringValue) {
90          return SearchUtils.newQuery(stringValue);
91      }
92  
93      /**
94       * @param fieldName field name string
95       * @param op        operation
96       * @param values    array of values
97       * @return a query based on the fields, operation and values.
98       */
99      public QueryExpression newQuery(String fieldName, Operation op, Object... values) {
100         return SearchUtils.newQuery(fieldName, op, Arrays.asList(values));
101     }
102 
103 }