com.meltmedia.cadmium.search.SearchService Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2012 meltmedia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.meltmedia.cadmium.search;
import com.google.inject.Inject;
import com.meltmedia.cadmium.core.CadmiumApiEndpoint;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLEncoder;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@CadmiumApiEndpoint
@Path("/search")
public class SearchService
{
private final Logger logger = LoggerFactory.getLogger(getClass());
private static final String ALLOWED_CHARS_PATTERN = "((\\-)|(\\()|(\\))|(\\\\))";
@Inject
protected IndexSearcherProvider provider;
@GET
@Produces("application/json")
public Map search(@QueryParam("query") String query,@QueryParam("path") String path)
throws Exception {
Map resultMap = buildSearchResults(query, path);
return resultMap;
}
private Map buildSearchResults(final String query, final String path) throws Exception {
logger.info("Running search for [{}]", query);
final Map resultMap = new LinkedHashMap();
new SearchTemplate(provider) {
public void doSearch(IndexSearcher index) throws IOException,
ParseException {
QueryParser parser = createParser(getAnalyzer());
resultMap.put("number-hits", 0);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy