Search in sources :

Example 11 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 12 with ParsedQuery

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

the class DlsFlsValveImpl method handleSearchContext.

@Override
public void handleSearchContext(SearchContext context, ThreadPool threadPool, NamedXContentRegistry namedXContentRegistry) {
    try {
        final Map<String, Set<String>> queries = (Map<String, Set<String>>) HeaderHelper.deserializeSafeFromHeader(threadPool.getThreadContext(), ConfigConstants.OPENDISTRO_SECURITY_DLS_QUERY_HEADER);
        final String dlsEval = SecurityUtils.evalMap(queries, context.indexShard().indexSettings().getIndex().getName());
        if (dlsEval != null) {
            if (context.suggest() != null) {
                return;
            }
            assert context.parsedQuery() != null;
            final Set<String> unparsedDlsQueries = queries.get(dlsEval);
            if (unparsedDlsQueries != null && !unparsedDlsQueries.isEmpty()) {
                final ParsedQuery dlsQuery = DlsQueryParser.parse(unparsedDlsQueries, context.parsedQuery(), context.getQueryShardContext(), namedXContentRegistry);
                context.parsedQuery(dlsQuery);
                context.preProcess(true);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error evaluating dls for a search query: " + e, e);
    }
}
Also used : Set(java.util.Set) ParsedQuery(org.opensearch.index.query.ParsedQuery) Map(java.util.Map) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException)

Example 13 with ParsedQuery

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

the class PhraseSuggester method innerExecute.

/*
     * More Ideas:
     *   - add ability to find whitespace problems -> we can build a poor mans decompounder with our index based on a automaton?
     *   - add ability to build different error models maybe based on a confusion matrix?
     *   - try to combine a token with its subsequent token to find / detect word splits (optional)
     *      - for this to work we need some way to defined the position length of a candidate
     *   - phonetic filters could be interesting here too for candidate selection
     */
@Override
public Suggestion<? extends Entry<? extends Option>> innerExecute(String name, PhraseSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
    double realWordErrorLikelihood = suggestion.realworldErrorLikelihood();
    final PhraseSuggestion response = new PhraseSuggestion(name, suggestion.getSize());
    final IndexReader indexReader = searcher.getIndexReader();
    List<PhraseSuggestionContext.DirectCandidateGenerator> generators = suggestion.generators();
    final int numGenerators = generators.size();
    final List<CandidateGenerator> gens = new ArrayList<>(generators.size());
    for (int i = 0; i < numGenerators; i++) {
        PhraseSuggestionContext.DirectCandidateGenerator generator = generators.get(i);
        DirectSpellChecker directSpellChecker = generator.createDirectSpellChecker();
        Terms terms = MultiTerms.getTerms(indexReader, generator.field());
        if (terms != null) {
            gens.add(new DirectCandidateGenerator(directSpellChecker, generator.field(), generator.suggestMode(), indexReader, realWordErrorLikelihood, generator.size(), generator.preFilter(), generator.postFilter(), terms));
        }
    }
    final String suggestField = suggestion.getField();
    final Terms suggestTerms = MultiTerms.getTerms(indexReader, suggestField);
    if (gens.size() > 0 && suggestTerms != null) {
        final NoisyChannelSpellChecker checker = new NoisyChannelSpellChecker(realWordErrorLikelihood, suggestion.getRequireUnigram(), suggestion.getTokenLimit());
        final BytesRef separator = suggestion.separator();
        WordScorer wordScorer = suggestion.model().newScorer(indexReader, suggestTerms, suggestField, realWordErrorLikelihood, separator);
        Result checkerResult;
        try (TokenStream stream = tokenStream(suggestion.getAnalyzer(), suggestion.getText(), spare, suggestion.getField())) {
            checkerResult = checker.getCorrections(stream, new MultiCandidateGeneratorWrapper(suggestion.getShardSize(), gens.toArray(new CandidateGenerator[gens.size()])), suggestion.maxErrors(), suggestion.getShardSize(), wordScorer, suggestion.confidence(), suggestion.gramSize());
        }
        PhraseSuggestion.Entry resultEntry = buildResultEntry(suggestion, spare, checkerResult.cutoffScore);
        response.addTerm(resultEntry);
        final BytesRefBuilder byteSpare = new BytesRefBuilder();
        final TemplateScript.Factory scriptFactory = suggestion.getCollateQueryScript();
        final boolean collatePrune = (scriptFactory != null) && suggestion.collatePrune();
        for (int i = 0; i < checkerResult.corrections.length; i++) {
            Correction correction = checkerResult.corrections[i];
            spare.copyUTF8Bytes(correction.join(SEPARATOR, byteSpare, null, null));
            boolean collateMatch = true;
            if (scriptFactory != null) {
                // Checks if the template query collateScript yields any documents
                // from the index for a correction, collateMatch is updated
                final Map<String, Object> vars = suggestion.getCollateScriptParams();
                vars.put(SUGGESTION_TEMPLATE_VAR_NAME, spare.toString());
                QueryShardContext shardContext = suggestion.getShardContext();
                final String querySource = scriptFactory.newInstance(vars).execute();
                try (XContentParser parser = XContentFactory.xContent(querySource).createParser(shardContext.getXContentRegistry(), LoggingDeprecationHandler.INSTANCE, querySource)) {
                    QueryBuilder innerQueryBuilder = AbstractQueryBuilder.parseInnerQueryBuilder(parser);
                    final ParsedQuery parsedQuery = shardContext.toQuery(innerQueryBuilder);
                    collateMatch = Lucene.exists(searcher, parsedQuery.query());
                }
            }
            if (!collateMatch && !collatePrune) {
                continue;
            }
            Text phrase = new Text(spare.toString());
            Text highlighted = null;
            if (suggestion.getPreTag() != null) {
                spare.copyUTF8Bytes(correction.join(SEPARATOR, byteSpare, suggestion.getPreTag(), suggestion.getPostTag()));
                highlighted = new Text(spare.toString());
            }
            if (collatePrune) {
                resultEntry.addOption(new PhraseSuggestion.Entry.Option(phrase, highlighted, (float) (correction.score), collateMatch));
            } else {
                resultEntry.addOption(new PhraseSuggestion.Entry.Option(phrase, highlighted, (float) (correction.score)));
            }
        }
    } else {
        response.addTerm(buildResultEntry(suggestion, spare, Double.MIN_VALUE));
    }
    return response;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) ParsedQuery(org.opensearch.index.query.ParsedQuery) ArrayList(java.util.ArrayList) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) Result(org.opensearch.search.suggest.phrase.NoisyChannelSpellChecker.Result) Entry(org.opensearch.search.suggest.Suggest.Suggestion.Entry) QueryShardContext(org.opensearch.index.query.QueryShardContext) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker) BytesRef(org.apache.lucene.util.BytesRef) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Terms(org.apache.lucene.index.Terms) MultiTerms(org.apache.lucene.index.MultiTerms) Text(org.opensearch.common.text.Text) IndexReader(org.apache.lucene.index.IndexReader) TemplateScript(org.opensearch.script.TemplateScript) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 14 with ParsedQuery

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

the class QueryPhaseTests method testCancellationDuringPreprocess.

public void testCancellationDuringPreprocess() throws IOException {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig())) {
        for (int i = 0; i < 10; i++) {
            Document doc = new Document();
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < i; j++) {
                sb.append('a');
            }
            doc.add(new StringField("foo", sb.toString(), Store.NO));
            w.addDocument(doc);
        }
        w.flush();
        w.close();
        try (IndexReader reader = DirectoryReader.open(dir)) {
            TestSearchContext context = new TestSearchContextWithRewriteAndCancellation(null, indexShard, newContextSearcher(reader));
            PrefixQuery prefixQuery = new PrefixQuery(new Term("foo", "a"));
            prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
            context.parsedQuery(new ParsedQuery(prefixQuery));
            SearchShardTask task = mock(SearchShardTask.class);
            when(task.isCancelled()).thenReturn(true);
            context.setTask(task);
            expectThrows(TaskCancelledException.class, () -> new QueryPhase().preProcess(context));
        }
    }
}
Also used : 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) PrefixQuery(org.apache.lucene.search.PrefixQuery) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Example 15 with ParsedQuery

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

the class QueryPhaseTests method testQueryCapturesThreadPoolStats.

public void testQueryCapturesThreadPoolStats() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig();
    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);
    TestSearchContext context = new TestSearchContext(null, indexShard, newContextSearcher(reader));
    context.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    QueryPhase.executeInternal(context);
    QuerySearchResult results = context.queryResult();
    assertThat(results.serviceTimeEWMA(), greaterThanOrEqualTo(0L));
    assertThat(results.nodeQueueSize(), greaterThanOrEqualTo(0));
    reader.close();
    dir.close();
}
Also used : TestSearchContext(org.opensearch.test.TestSearchContext) ParsedQuery(org.opensearch.index.query.ParsedQuery) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) LatLonPoint(org.apache.lucene.document.LatLonPoint) LongPoint(org.apache.lucene.document.LongPoint) 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)20 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 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)12 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)11 LatLonPoint (org.apache.lucene.document.LatLonPoint)10 LongPoint (org.apache.lucene.document.LongPoint)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 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 IOException (java.io.IOException)4 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)4 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)4 PrefixQuery (org.apache.lucene.search.PrefixQuery)4