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