private void countTestCase(Query query, IndexReader reader, boolean shouldCollect) throws Exception { TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(query)); context.setSize(0); context.setTask(new SearchTask(123L, "", "", "", null)); IndexSearcher searcher = new IndexSearcher(reader); final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(reader) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; final boolean rescore = QueryPhase.execute(context, contextSearcher); assertFalse(rescore); assertEquals(searcher.count(query), context.queryResult().topDocs().totalHits); assertEquals(shouldCollect, collected.get()); }
public void testPostFilterDisablesCountOptimization() throws Exception { TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0); context.setTask(new SearchTask(123L, "", "", "", null)); final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertFalse(collected.get()); context.parsedPostFilter(new ParsedQuery(new MatchNoDocsQuery())); QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertTrue(collected.get()); }
public void testMinScoreDisablesCountOptimization() throws Exception { TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0); context.setTask(new SearchTask(123L, "", "", "", null)); final AtomicBoolean collected = new AtomicBoolean(); IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) { protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException { collected.set(true); super.search(leaves, weight, collector); } }; QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertFalse(collected.get()); context.minimumScore(1); QueryPhase.execute(context, contextSearcher); assertEquals(0, context.queryResult().topDocs().totalHits); assertTrue(collected.get()); }
public CrateSearchContext createContext( int jobSearchContextId, IndexShard indexshard, Engine.Searcher engineSearcher, WhereClause whereClause) { ShardId shardId = indexshard.shardId(); SearchShardTarget searchShardTarget = new SearchShardTarget( clusterService.state().nodes().localNodeId(), shardId.getIndex(), shardId.id() ); IndexService indexService = indexshard.indexService(); CrateSearchContext searchContext = new CrateSearchContext( jobSearchContextId, System.currentTimeMillis(), searchShardTarget, engineSearcher, indexService, indexshard, scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), Optional.<Scroll>absent() ); LuceneQueryBuilder.Context context = luceneQueryBuilder.convert( whereClause, indexService.mapperService(), indexService.fieldData(), indexService.cache()); searchContext.parsedQuery(new ParsedQuery(context.query(), EMPTY_NAMED_FILTERS)); Float minScore = context.minScore(); if (minScore != null) { searchContext.minimumScore(minScore); } return searchContext; }
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (context.highlight() != null) { context.parsedQuery(new ParsedQuery(query)); context.hitContext().cache().clear(); } if (Lucene.exists(searcher, existsQuery)) { if (!limit || counter < size) { matches.add(BytesRef.deepCopyOf(current)); if (context.highlight() != null) { highlightPhase.hitExecute(context, context.hitContext()); hls.add(context.hitContext().hit().getHighlightFields()); } } counter++; postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (context.highlight() != null) { context.parsedQuery(new ParsedQuery(query)); context.hitContext().cache().clear(); } if (Lucene.exists(searcher, existsQuery)) { if (!limit || counter < size) { matches.add(BytesRef.deepCopyOf(current)); scores.add(scorer.score()); if (context.highlight() != null) { highlightPhase.hitExecute(context, context.hitContext()); hls.add(context.hitContext().hit().getHighlightFields()); } } counter++; postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
@Override public void parse(XContentParser parser, SearchContext context) throws Exception { ParsedQuery postFilter = context.queryParserService().parseInnerFilter(parser); if (postFilter != null) { context.parsedPostFilter(postFilter); } }
@Override public void parse(XContentParser parser, SearchContext context) throws Exception { byte[] filterSource = parser.binaryValue(); try (XContentParser fSourceParser = XContentFactory.xContent(filterSource).createParser(filterSource)) { ParsedQuery filter = context.queryParserService().parseInnerFilter(fSourceParser); if (filter != null) { context.parsedPostFilter(filter); } } }
protected BaseInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits) { super(context); this.query = query; if (childInnerHits != null && !childInnerHits.isEmpty()) { this.childInnerHits = new InnerHitsContext(childInnerHits); } else { this.childInnerHits = null; } }
/** * Should be called before executing the main query and after all other parameters have been set. */ @Override public void preProcess() { if (scrollContext == null) { long from = from() == -1 ? 0 : from(); long size = size() == -1 ? 10 : size(); long resultWindow = from + size; // We need settingsService's view of the settings because its dynamic. // indexService's isn't. int maxResultWindow = indexService.indexSettings().getAsInt(MAX_RESULT_WINDOW, Defaults.MAX_RESULT_WINDOW); if (resultWindow > maxResultWindow) { throw new QueryPhaseExecutionException(this, "Result window is too large, from + size must be less than or equal to: [" + maxResultWindow + "] but was [" + resultWindow + "]. See the scroll api for a more efficient way to request large data sets. " + "This limit can be set by changing the [" + DefaultSearchContext.MAX_RESULT_WINDOW + "] index level parameter."); } } // initialize the filtering alias based on the provided filters aliasFilter = indexService.aliasesService().aliasFilter(request.filteringAliases()); if (query() == null) { parsedQuery(ParsedQuery.parsedMatchAllQuery()); } if (queryBoost() != 1.0f) { parsedQuery(new ParsedQuery(new FunctionScoreQuery(query(), new BoostScoreFunction(queryBoost)), parsedQuery())); } filteredQuery(buildFilteredQuery()); try { this.query = searcher().rewrite(this.query); } catch (IOException e) { throw new QueryPhaseExecutionException(this, "Failed to rewrite main query", e); } }
@Override public RescoreSearchContext parse(XContentParser parser, SearchContext context) throws IOException { Token token; String fieldName = null; QueryRescoreContext rescoreContext = new QueryRescoreContext(this); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); if ("rescore_query".equals(fieldName)) { ParsedQuery parsedQuery = context.queryParserService().parse(parser); rescoreContext.setParsedQuery(parsedQuery); } } else if (token.isValue()) { if ("query_weight".equals(fieldName)) { rescoreContext.setQueryWeight(parser.floatValue()); } else if ("rescore_query_weight".equals(fieldName)) { rescoreContext.setRescoreQueryWeight(parser.floatValue()); } else if ("score_mode".equals(fieldName)) { String sScoreMode = parser.text(); if ("avg".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Avg); } else if ("max".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Max); } else if ("min".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Min); } else if ("total".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Total); } else if ("multiply".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Multiply); } else { throw new IllegalArgumentException("[rescore] illegal score_mode [" + sScoreMode + "]"); } } else { throw new IllegalArgumentException("rescore doesn't support [" + fieldName + "]"); } } } return rescoreContext; }
private static Engine.DeleteByQuery prepareDeleteByQuery(IndexQueryParserService queryParserService, MapperService mapperService, IndexAliasesService indexAliasesService, IndexCache indexCache, BytesReference source, @Nullable String[] filteringAliases, Engine.Operation.Origin origin, String... types) { long startTime = System.nanoTime(); if (types == null) { types = Strings.EMPTY_ARRAY; } Query query; try { query = queryParserService.parseQuery(source).query(); } catch (QueryParsingException ex) { // for BWC we try to parse directly the query since pre 1.0.0.Beta2 we didn't require a top level query field if (queryParserService.getIndexCreatedVersion().onOrBefore(Version.V_1_0_0_Beta2)) { try { XContentParser parser = XContentHelper.createParser(source); ParsedQuery parse = queryParserService.parse(parser); query = parse.query(); } catch (Throwable t) { ex.addSuppressed(t); throw ex; } } else { throw ex; } } Query searchFilter = mapperService.searchFilter(types); if (searchFilter != null) { query = Queries.filtered(query, searchFilter); } Query aliasFilter = indexAliasesService.aliasFilter(filteringAliases); BitSetProducer parentFilter = mapperService.hasNested() ? indexCache.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter()) : null; return new Engine.DeleteByQuery(query, source, filteringAliases, aliasFilter, parentFilter, origin, startTime, types); }
Query parse(AliasMetaData alias) { if (alias.filter() == null) { return null; } try { byte[] filterSource = alias.filter().uncompressed(); try (XContentParser parser = XContentFactory.xContent(filterSource).createParser(filterSource)) { ParsedQuery parsedFilter = indexQueryParser.parseInnerFilter(parser); return parsedFilter == null ? null : parsedFilter.query(); } } catch (IOException ex) { throw new AliasFilterParsingException(index, alias.getAlias(), "Invalid alias filter", ex); } }
@Override public SearchContext parsedPostFilter(ParsedQuery postFilter) { this.postFilter = postFilter; return this; }
@Override public ParsedQuery parsedPostFilter() { return postFilter; }
@Override public SearchContext parsedQuery(ParsedQuery query) { this.originalQuery = query; this.query = query.query(); return this; }
@Override public ParsedQuery parsedQuery() { return originalQuery; }
@Override public SearchContext parsedPostFilter(ParsedQuery postFilter) { return in.parsedPostFilter(postFilter); }
@Override public ParsedQuery parsedPostFilter() { return in.parsedPostFilter(); }
@Override public SearchContext parsedQuery(ParsedQuery query) { return in.parsedQuery(query); }
@Override public ParsedQuery parsedQuery() { return in.parsedQuery(); }
@Override public ParsedQuery parsedQuery() { return parsedQuery; }
@Override public SearchContext parsedPostFilter(ParsedQuery postFilter) { throw new UnsupportedOperationException("Not supported"); }
@Override public ParsedQuery parsedPostFilter() { return this.postFilter; }
@Override public ParsedQuery parsedQuery() { return this.originalQuery; }
@Override public SearchContext parsedQuery(ParsedQuery query) { this.parsedQuery = query; this.query = query.query(); return this; }
@Override public SearchContext parsedPostFilter(ParsedQuery postFilter) { throw new UnsupportedOperationException(); }
@Override public ParsedQuery parsedPostFilter() { return null; }
@Override public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException { ParsedQuery filter = context.queryParserService().parseInnerFilter(parser); return new FilterAggregator.Factory(aggregationName, filter == null ? new MatchAllDocsQuery() : filter.query()); }
@Override public ParsedQuery parsedQuery() { return query; }
public NestedInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, ObjectMapper parentObjectMapper, ObjectMapper childObjectMapper) { super(context, query, childInnerHits); this.parentObjectMapper = parentObjectMapper; this.childObjectMapper = childObjectMapper; }
public ParentChildInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, MapperService mapperService, DocumentMapper documentMapper) { super(context, query, childInnerHits); this.mapperService = mapperService; this.documentMapper = documentMapper; }
private ParseResult(SubSearchContext context, ParsedQuery query, Map<String, InnerHitsContext.BaseInnerHits> childInnerHits) { this.context = context; this.query = query; this.childInnerHits = childInnerHits; }
public ParsedQuery query() { return query; }