use of com.xiaomi.linden.lucene.merge.SortingMergePolicyDecorator in project linden by XiaoMi.
the class LindenCoreImpl method search.
public LindenResult search(LindenSearchRequest request) throws IOException {
SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy = lindenNRTSearcherManager.acquire();
try {
IndexSearcher indexSearcher = searcherAndTaxonomy.searcher;
Filter filter = FilterConstructor.constructFilter(request.getFilter(), config);
Sort sort = SortConstructor.constructSort(request, indexSearcher, config);
indexSearcher.setSimilarity(config.getSearchSimilarityInstance());
Query query = QueryConstructor.constructQuery(request.getQuery(), config);
if (filter != null) {
query = new FilteredQuery(query, filter);
}
int from = request.getOffset();
int size = request.getLength();
LindenResultParser resultParser = new LindenResultParser(config, request, indexSearcher, snippetGenerator, query, filter, sort);
// very common search, no group, no facet, no early termination, no search time limit
if (!request.isSetGroupParam() && !request.isSetFacet() && !request.isSetEarlyParam() && config.getSearchTimeLimit() <= 0) {
TopDocs docs;
if (sort != null) {
docs = indexSearcher.search(query, from + size, sort);
} else {
docs = indexSearcher.search(query, from + size);
}
return resultParser.parse(docs, null, null, null);
}
// group param will suppress facet, group, early termination and search time limit parameters
if (request.isSetGroupParam()) {
String groupField = request.getGroupParam().getGroupField();
GroupingSearch groupingSearch = new GroupingSearch(groupField);
groupingSearch.setGroupDocsLimit(request.getGroupParam().getGroupInnerLimit());
if (sort != null) {
groupingSearch.setGroupSort(sort);
groupingSearch.setSortWithinGroup(sort);
groupingSearch.setFillSortFields(true);
}
groupingSearch.setCachingInMB(8.0, true);
groupingSearch.setAllGroups(true);
TopGroups<TopDocs> topGroupedDocs = groupingSearch.search(indexSearcher, query, 0, from + size);
return resultParser.parse(null, topGroupedDocs, null, null);
}
TopDocsCollector topDocsCollector;
if (sort != null) {
topDocsCollector = TopFieldCollector.create(sort, from + size, null, true, false, false, false);
} else {
topDocsCollector = TopScoreDocCollector.create(from + size, false);
}
LindenDocsCollector lindenDocsCollector;
if (request.isSetEarlyParam()) {
MergePolicy mergePolicy = indexWriter.getConfig().getMergePolicy();
Sort mergePolicySort = null;
if (mergePolicy instanceof SortingMergePolicyDecorator) {
mergePolicySort = ((SortingMergePolicyDecorator) mergePolicy).getSort();
}
EarlyTerminationCollector earlyTerminationCollector = new EarlyTerminationCollector(topDocsCollector, mergePolicySort, request.getEarlyParam().getMaxNum());
lindenDocsCollector = new LindenDocsCollector(earlyTerminationCollector);
} else {
lindenDocsCollector = new LindenDocsCollector(topDocsCollector);
}
Collector collector = lindenDocsCollector;
if (config.getSearchTimeLimit() > 0) {
collector = new TimeLimitingCollector(lindenDocsCollector, TimeLimitingCollector.getGlobalCounter(), config.getSearchTimeLimit());
}
// no facet param
if (!request.isSetFacet()) {
indexSearcher.search(query, collector);
return resultParser.parse(lindenDocsCollector.topDocs(), null, null, null);
}
// facet search
LindenFacet facetRequest = request.getFacet();
FacetsCollector facetsCollector = new FacetsCollector();
lindenDocsCollector.wrap(facetsCollector);
Facets facets = null;
if (facetRequest.isSetDrillDownDimAndPaths()) {
// drillDown or drillSideways
DrillDownQuery drillDownQuery = new DrillDownQuery(facetsConfig, query);
List<LindenFacetDimAndPath> drillDownDimAndPaths = facetRequest.getDrillDownDimAndPaths();
for (int i = 0; i < drillDownDimAndPaths.size(); ++i) {
String fieldName = drillDownDimAndPaths.get(i).dim;
if (drillDownDimAndPaths.get(i).path != null) {
drillDownQuery.add(fieldName, drillDownDimAndPaths.get(i).path.split("/"));
} else {
drillDownQuery.add(fieldName);
}
}
// drillSideways
if (facetRequest.getFacetDrillingType() == FacetDrillingType.DRILLSIDEWAYS) {
DrillSideways dillSideways = new DrillSideways(indexSearcher, facetsConfig, searcherAndTaxonomy.taxonomyReader);
DrillSideways.DrillSidewaysResult drillSidewaysResult = dillSideways.search(drillDownQuery, collector);
facets = drillSidewaysResult.facets;
} else {
// drillDown
indexSearcher.search(drillDownQuery, collector);
facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig, facetsCollector);
}
} else {
indexSearcher.search(query, collector);
// Simple facet browsing
if (facetRequest.isSetFacetParams()) {
facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig, facetsCollector);
}
}
return resultParser.parse(lindenDocsCollector.topDocs(), null, facets, facetsCollector);
} catch (Exception e) {
throw new IOException(Throwables.getStackTraceAsString(e));
} finally {
lindenNRTSearcherManager.release(searcherAndTaxonomy);
}
}
Aggregations