Search in sources :

Example 1 with ParsedQuery

use of org.opensearch.index.query.ParsedQuery in project OpenSearch by opensearch-project.

the class TransportValidateQueryAction method shardOperation.

@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request, Task task) throws IOException {
    boolean valid;
    String explanation = null;
    String error = null;
    ShardSearchRequest shardSearchLocalRequest = new ShardSearchRequest(request.shardId(), request.nowInMillis(), request.filteringAliases());
    SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
    try {
        ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
        searchContext.parsedQuery(parsedQuery);
        searchContext.preProcess(request.rewrite());
        valid = true;
        explanation = explain(searchContext, request.rewrite());
    } catch (QueryShardException | ParsingException e) {
        valid = false;
        error = e.getDetailedMessage();
    } catch (AssertionError e) {
        valid = false;
        error = e.getMessage();
    } finally {
        Releasables.close(searchContext);
    }
    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
Also used : ParsedQuery(org.opensearch.index.query.ParsedQuery) ParsingException(org.opensearch.common.ParsingException) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with ParsedQuery

use of org.opensearch.index.query.ParsedQuery in project OpenSearch by opensearch-project.

the class DefaultSearchContext method preProcess.

/**
 * Should be called before executing the main query and after all other parameters have been set.
 */
@Override
public void preProcess(boolean rewrite) {
    if (hasOnlySuggest()) {
        return;
    }
    long from = from() == -1 ? 0 : from();
    long size = size() == -1 ? 10 : size();
    long resultWindow = from + size;
    int maxResultWindow = indexService.getIndexSettings().getMaxResultWindow();
    if (resultWindow > maxResultWindow) {
        if (scrollContext() == null) {
            throw new IllegalArgumentException("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 [" + IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey() + "] index level setting.");
        }
        throw new IllegalArgumentException("Batch size is too large, size must be less than or equal to: [" + maxResultWindow + "] but was [" + resultWindow + "]. Scroll batch sizes cost as much memory as result windows so they are controlled by the [" + IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey() + "] index level setting.");
    }
    if (rescore != null) {
        if (sort != null) {
            throw new IllegalArgumentException("Cannot use [sort] option in conjunction with [rescore].");
        }
        int maxWindow = indexService.getIndexSettings().getMaxRescoreWindow();
        for (RescoreContext rescoreContext : rescore()) {
            if (rescoreContext.getWindowSize() > maxWindow) {
                throw new IllegalArgumentException("Rescore window [" + rescoreContext.getWindowSize() + "] is too large. " + "It must be less than [" + maxWindow + "]. This prevents allocating massive heaps for storing the results " + "to be rescored. This limit can be set by changing the [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.getKey() + "] index level setting.");
            }
        }
    }
    if (sliceBuilder != null) {
        int sliceLimit = indexService.getIndexSettings().getMaxSlicesPerScroll();
        int numSlices = sliceBuilder.getMax();
        if (numSlices > sliceLimit) {
            throw new IllegalArgumentException("The number of slices [" + numSlices + "] is too large. It must " + "be less than [" + sliceLimit + "]. This limit can be set by changing the [" + IndexSettings.MAX_SLICES_PER_SCROLL.getKey() + "] index level setting.");
        }
    }
    // initialize the filtering alias based on the provided filters
    try {
        final QueryBuilder queryBuilder = request.getAliasFilter().getQueryBuilder();
        aliasFilter = queryBuilder == null ? null : queryBuilder.toQuery(queryShardContext);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    if (query() == null) {
        parsedQuery(ParsedQuery.parsedMatchAllQuery());
    }
    if (queryBoost() != AbstractQueryBuilder.DEFAULT_BOOST) {
        parsedQuery(new ParsedQuery(new BoostQuery(query(), queryBoost), parsedQuery()));
    }
    this.query = buildFilteredQuery(query);
    if (rewrite) {
        try {
            this.query = searcher.rewrite(query);
        } catch (IOException e) {
            throw new QueryPhaseExecutionException(shardTarget, "Failed to rewrite main query", e);
        }
    }
}
Also used : RescoreContext(org.opensearch.search.rescore.RescoreContext) ParsedQuery(org.opensearch.index.query.ParsedQuery) QueryPhaseExecutionException(org.opensearch.search.query.QueryPhaseExecutionException) UncheckedIOException(java.io.UncheckedIOException) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 3 with ParsedQuery

use of org.opensearch.index.query.ParsedQuery in project OpenSearch by opensearch-project.

the class QueryPhaseTests method testMinScore.

public void testMinScore() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new StringField("foo", "bar", Store.NO));
        doc.add(new StringField("filter", "f1", Store.NO));
        w.addDocument(doc);
    }
    w.close();
    IndexReader reader = DirectoryReader.open(dir);
    TestSearchContext context = new TestSearchContext(null, indexShard, newContextSearcher(reader));
    context.parsedQuery(new ParsedQuery(new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("filter", "f1")), Occur.SHOULD).build()));
    context.minimumScore(0.01f);
    context.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
    context.setSize(1);
    context.trackTotalHitsUpTo(5);
    QueryPhase.executeInternal(context);
    assertEquals(10, context.queryResult().topDocs().topDocs.totalHits.value);
    reader.close();
    dir.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) ParsedQuery(org.opensearch.index.query.ParsedQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) LatLonPoint(org.apache.lucene.document.LatLonPoint) LongPoint(org.apache.lucene.document.LongPoint) TestSearchContext(org.opensearch.test.TestSearchContext) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Example 4 with ParsedQuery

use of org.opensearch.index.query.ParsedQuery in project OpenSearch by opensearch-project.

the class QueryPhaseTests method testInOrderScrollOptimization.

public void testInOrderScrollOptimization() throws Exception {
    Directory dir = newDirectory();
    final Sort sort = new Sort(new SortField("rank", SortField.Type.INT));
    IndexWriterConfig iwc = newIndexWriterConfig().setIndexSort(sort);
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    final int numDocs = scaledRandomIntBetween(100, 200);
    for (int i = 0; i < numDocs; ++i) {
        w.addDocument(new Document());
    }
    w.close();
    IndexReader reader = DirectoryReader.open(dir);
    ScrollContext scrollContext = new ScrollContext();
    TestSearchContext context = new TestSearchContext(null, indexShard, newContextSearcher(reader), scrollContext);
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    scrollContext.lastEmittedDoc = null;
    scrollContext.maxScore = Float.NaN;
    scrollContext.totalHits = null;
    context.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
    int size = randomIntBetween(2, 5);
    context.setSize(size);
    QueryPhase.executeInternal(context);
    assertThat(context.queryResult().topDocs().topDocs.totalHits.value, equalTo((long) numDocs));
    assertNull(context.queryResult().terminatedEarly());
    assertThat(context.terminateAfter(), equalTo(0));
    assertThat(context.queryResult().getTotalHits().value, equalTo((long) numDocs));
    context.setSearcher(newEarlyTerminationContextSearcher(reader, size));
    QueryPhase.executeInternal(context);
    assertThat(context.queryResult().topDocs().topDocs.totalHits.value, equalTo((long) numDocs));
    assertThat(context.terminateAfter(), equalTo(size));
    assertThat(context.queryResult().getTotalHits().value, equalTo((long) numDocs));
    assertThat(context.queryResult().topDocs().topDocs.scoreDocs[0].doc, greaterThanOrEqualTo(size));
    reader.close();
    dir.close();
}
Also used : ParsedQuery(org.opensearch.index.query.ParsedQuery) ScrollContext(org.opensearch.search.internal.ScrollContext) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LatLonPoint(org.apache.lucene.document.LatLonPoint) LongPoint(org.apache.lucene.document.LongPoint) TestSearchContext(org.opensearch.test.TestSearchContext) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Example 5 with ParsedQuery

use of org.opensearch.index.query.ParsedQuery in project OpenSearch by opensearch-project.

the class QueryPhaseTests method testIndexSortingEarlyTermination.

public void testIndexSortingEarlyTermination() throws Exception {
    Directory dir = newDirectory();
    final Sort sort = new Sort(new SortField("rank", SortField.Type.INT));
    IndexWriterConfig iwc = newIndexWriterConfig().setIndexSort(sort);
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    final int numDocs = scaledRandomIntBetween(100, 200);
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        if (randomBoolean()) {
            doc.add(new StringField("foo", "bar", Store.NO));
        }
        if (randomBoolean()) {
            doc.add(new StringField("foo", "baz", Store.NO));
        }
        doc.add(new NumericDocValuesField("rank", numDocs - i));
        w.addDocument(doc);
    }
    w.close();
    final IndexReader reader = DirectoryReader.open(dir);
    TestSearchContext context = new TestSearchContext(null, indexShard, newContextSearcher(reader));
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    context.setSize(1);
    context.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
    context.sort(new SortAndFormats(sort, new DocValueFormat[] { DocValueFormat.RAW }));
    QueryPhase.executeInternal(context);
    assertThat(context.queryResult().topDocs().topDocs.totalHits.value, equalTo((long) numDocs));
    assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(1));
    assertThat(context.queryResult().topDocs().topDocs.scoreDocs[0], instanceOf(FieldDoc.class));
    FieldDoc fieldDoc = (FieldDoc) context.queryResult().topDocs().topDocs.scoreDocs[0];
    assertThat(fieldDoc.fields[0], equalTo(1));
    {
        context.parsedPostFilter(new ParsedQuery(new MinDocQuery(1)));
        QueryPhase.executeInternal(context);
        assertNull(context.queryResult().terminatedEarly());
        assertThat(context.queryResult().topDocs().topDocs.totalHits.value, equalTo(numDocs - 1L));
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(1));
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs[0], instanceOf(FieldDoc.class));
        assertThat(fieldDoc.fields[0], anyOf(equalTo(1), equalTo(2)));
        context.parsedPostFilter(null);
        final TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
        context.queryCollectors().put(TotalHitCountCollector.class, totalHitCountCollector);
        QueryPhase.executeInternal(context);
        assertNull(context.queryResult().terminatedEarly());
        assertThat(context.queryResult().topDocs().topDocs.totalHits.value, equalTo((long) numDocs));
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(1));
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs[0], instanceOf(FieldDoc.class));
        assertThat(fieldDoc.fields[0], anyOf(equalTo(1), equalTo(2)));
        assertThat(totalHitCountCollector.getTotalHits(), equalTo(numDocs));
        context.queryCollectors().clear();
    }
    {
        context.setSearcher(newEarlyTerminationContextSearcher(reader, 1));
        context.trackTotalHitsUpTo(SearchContext.TRACK_TOTAL_HITS_DISABLED);
        QueryPhase.executeInternal(context);
        assertNull(context.queryResult().terminatedEarly());
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(1));
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs[0], instanceOf(FieldDoc.class));
        assertThat(fieldDoc.fields[0], anyOf(equalTo(1), equalTo(2)));
        QueryPhase.executeInternal(context);
        assertNull(context.queryResult().terminatedEarly());
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(1));
        assertThat(context.queryResult().topDocs().topDocs.scoreDocs[0], instanceOf(FieldDoc.class));
        assertThat(fieldDoc.fields[0], anyOf(equalTo(1), equalTo(2)));
    }
    reader.close();
    dir.close();
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) ParsedQuery(org.opensearch.index.query.ParsedQuery) DocValueFormat(org.opensearch.search.DocValueFormat) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortAndFormats(org.opensearch.search.sort.SortAndFormats) LatLonPoint(org.apache.lucene.document.LatLonPoint) LongPoint(org.apache.lucene.document.LongPoint) TestSearchContext(org.opensearch.test.TestSearchContext) MinDocQuery(org.apache.lucene.queries.MinDocQuery) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Aggregations

ParsedQuery (org.opensearch.index.query.ParsedQuery)17 IndexReader (org.apache.lucene.index.IndexReader)14 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)13 Directory (org.apache.lucene.store.Directory)13 SearchShardTask (org.opensearch.action.search.SearchShardTask)13 TestSearchContext (org.opensearch.test.TestSearchContext)13 Document (org.apache.lucene.document.Document)12 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)11 LatLonPoint (org.apache.lucene.document.LatLonPoint)10 LongPoint (org.apache.lucene.document.LongPoint)10 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)10 Sort (org.apache.lucene.search.Sort)9 SortField (org.apache.lucene.search.SortField)8 StringField (org.apache.lucene.document.StringField)5 Term (org.apache.lucene.index.Term)5 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)4 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 DocValueFormat (org.opensearch.search.DocValueFormat)4