Search in sources :

Example 1 with SimpleQueryStringQueryParser

use of org.opensearch.index.search.SimpleQueryStringQueryParser in project OpenSearch by opensearch-project.

the class SimpleQueryStringBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Settings newSettings = new Settings(settings);
    final Map<String, Float> resolvedFieldsAndWeights;
    boolean isAllField;
    if (fieldsAndWeights.isEmpty() == false) {
        resolvedFieldsAndWeights = QueryParserHelper.resolveMappingFields(context, fieldsAndWeights);
        isAllField = QueryParserHelper.hasAllFieldsWildcard(fieldsAndWeights.keySet());
    } else {
        List<String> defaultFields = context.defaultFields();
        resolvedFieldsAndWeights = QueryParserHelper.resolveMappingFields(context, QueryParserHelper.parseFieldsAndWeights(defaultFields));
        isAllField = QueryParserHelper.hasAllFieldsWildcard(defaultFields);
    }
    if (isAllField) {
        newSettings.lenient(lenientSet ? settings.lenient() : true);
    }
    final SimpleQueryStringQueryParser sqp;
    if (analyzer == null) {
        sqp = new SimpleQueryStringQueryParser(resolvedFieldsAndWeights, flags, newSettings, context);
    } else {
        Analyzer luceneAnalyzer = context.getIndexAnalyzers().get(analyzer);
        if (luceneAnalyzer == null) {
            throw new QueryShardException(context, "[" + SimpleQueryStringBuilder.NAME + "] analyzer [" + analyzer + "] not found");
        }
        sqp = new SimpleQueryStringQueryParser(luceneAnalyzer, resolvedFieldsAndWeights, flags, newSettings, context);
    }
    sqp.setDefaultOperator(defaultOperator.toBooleanClauseOccur());
    Query query = sqp.parse(queryText);
    return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}
Also used : SimpleQueryStringQueryParser(org.opensearch.index.search.SimpleQueryStringQueryParser) Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Analyzer(org.apache.lucene.analysis.Analyzer) Settings(org.opensearch.index.search.SimpleQueryStringQueryParser.Settings)

Example 2 with SimpleQueryStringQueryParser

use of org.opensearch.index.search.SimpleQueryStringQueryParser in project OpenSearch by opensearch-project.

the class SimpleQueryStringBuilderTests method testAnalyzerWithGraph.

public void testAnalyzerWithGraph() {
    SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings();
    settings.analyzeWildcard(true);
    SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new MockSynonymAnalyzer(), Collections.singletonMap(TEXT_FIELD_NAME, 1.0f), -1, settings, createShardContext());
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        // non-phrase won't detect multi-word synonym because of whitespace splitting
        Query query = parser.parse("guinea pig");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "guinea")), defaultOp)).add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "pig")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
        // phrase will pick it up
        query = parser.parse("\"guinea pig\"");
        SpanTermQuery span1 = new SpanTermQuery(new Term(TEXT_FIELD_NAME, "guinea"));
        SpanTermQuery span2 = new SpanTermQuery(new Term(TEXT_FIELD_NAME, "pig"));
        expectedQuery = new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term(TEXT_FIELD_NAME, "cavy")));
        assertThat(query, equalTo(expectedQuery));
        // phrase with slop
        query = parser.parse("big \"tiny guinea pig\"~2");
        PhraseQuery pq1 = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "tiny")).add(new Term(TEXT_FIELD_NAME, "guinea")).add(new Term(TEXT_FIELD_NAME, "pig")).setSlop(2).build();
        PhraseQuery pq2 = new PhraseQuery.Builder().add(new Term(TEXT_FIELD_NAME, "tiny")).add(new Term(TEXT_FIELD_NAME, "cavy")).setSlop(2).build();
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD_NAME, "big")), defaultOp).add(new BooleanQuery.Builder().add(pq1, BooleanClause.Occur.SHOULD).add(pq2, BooleanClause.Occur.SHOULD).build(), defaultOp).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SimpleQueryStringQueryParser(org.opensearch.index.search.SimpleQueryStringQueryParser) Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MockSynonymAnalyzer(org.apache.lucene.tests.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Settings(org.opensearch.common.settings.Settings)

Example 3 with SimpleQueryStringQueryParser

use of org.opensearch.index.search.SimpleQueryStringQueryParser in project OpenSearch by opensearch-project.

the class SimpleQueryStringBuilderTests method testAnalyzerWildcardWithSynonyms.

public void testAnalyzerWildcardWithSynonyms() throws IOException {
    SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings();
    settings.analyzeWildcard(true);
    SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new MockRepeatAnalyzer(), Collections.singletonMap(TEXT_FIELD_NAME, 1.0f), -1, settings, createShardContext());
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        Query query = parser.parse("first foo-bar-foobar* last");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery.Builder(TEXT_FIELD_NAME).addTerm(new Term(TEXT_FIELD_NAME, "first")).addTerm(new Term(TEXT_FIELD_NAME, "first")).build(), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new SynonymQuery.Builder(TEXT_FIELD_NAME).addTerm(new Term(TEXT_FIELD_NAME, "foo")).addTerm(new Term(TEXT_FIELD_NAME, "foo")).build(), defaultOp)).add(new BooleanClause(new SynonymQuery.Builder(TEXT_FIELD_NAME).addTerm(new Term(TEXT_FIELD_NAME, "bar")).addTerm(new Term(TEXT_FIELD_NAME, "bar")).build(), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new PrefixQuery(new Term(TEXT_FIELD_NAME, "foobar")), BooleanClause.Occur.SHOULD)).add(new BooleanClause(new PrefixQuery(new Term(TEXT_FIELD_NAME, "foobar")), BooleanClause.Occur.SHOULD)).build(), defaultOp).build(), defaultOp).add(new BooleanClause(new SynonymQuery.Builder(TEXT_FIELD_NAME).addTerm(new Term(TEXT_FIELD_NAME, "last")).addTerm(new Term(TEXT_FIELD_NAME, "last")).build(), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SimpleQueryStringQueryParser(org.opensearch.index.search.SimpleQueryStringQueryParser) Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) Settings(org.opensearch.common.settings.Settings)

Example 4 with SimpleQueryStringQueryParser

use of org.opensearch.index.search.SimpleQueryStringQueryParser in project OpenSearch by opensearch-project.

the class SimpleQueryStringBuilderTests method testAnalyzeWildcard.

public void testAnalyzeWildcard() throws IOException {
    SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings();
    settings.analyzeWildcard(true);
    SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new StandardAnalyzer(), Collections.singletonMap(TEXT_FIELD_NAME, 1.0f), -1, settings, createShardContext());
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        parser.setDefaultOperator(defaultOp);
        Query query = parser.parse("first foo-bar-foobar* last");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "first")), defaultOp)).add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "foo")), defaultOp)).add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), defaultOp)).add(new BooleanClause(new PrefixQuery(new Term(TEXT_FIELD_NAME, "foobar")), defaultOp)).build(), defaultOp).add(new BooleanClause(new TermQuery(new Term(TEXT_FIELD_NAME, "last")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SimpleQueryStringQueryParser(org.opensearch.index.search.SimpleQueryStringQueryParser) Query(org.apache.lucene.search.Query) SpanOrQuery(org.apache.lucene.queries.spans.SpanOrQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) SpanQuery(org.apache.lucene.queries.spans.SpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanNearQuery(org.apache.lucene.queries.spans.SpanNearQuery) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Settings(org.opensearch.common.settings.Settings)

Example 5 with SimpleQueryStringQueryParser

use of org.opensearch.index.search.SimpleQueryStringQueryParser in project OpenSearch by opensearch-project.

the class SimpleQueryStringBuilderTests method testQuoteFieldSuffix.

public void testQuoteFieldSuffix() {
    SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings();
    settings.analyzeWildcard(true);
    settings.quoteFieldSuffix("_2");
    SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new MockSynonymAnalyzer(), Collections.singletonMap(TEXT_FIELD_NAME, 1.0f), -1, settings, createShardContext());
    assertEquals(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), parser.parse("bar"));
    assertEquals(new TermQuery(new Term(KEYWORD_FIELD_NAME, "bar")), parser.parse("\"bar\""));
    // Now check what happens if the quote field does not exist
    settings.quoteFieldSuffix(".quote");
    parser = new SimpleQueryStringQueryParser(new MockSynonymAnalyzer(), Collections.singletonMap(TEXT_FIELD_NAME, 1.0f), -1, settings, createShardContext());
    assertEquals(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), parser.parse("bar"));
    assertEquals(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), parser.parse("\"bar\""));
}
Also used : SpanTermQuery(org.apache.lucene.queries.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SimpleQueryStringQueryParser(org.opensearch.index.search.SimpleQueryStringQueryParser) MockSynonymAnalyzer(org.apache.lucene.tests.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) Settings(org.opensearch.common.settings.Settings)

Aggregations

SimpleQueryStringQueryParser (org.opensearch.index.search.SimpleQueryStringQueryParser)5 Term (org.apache.lucene.index.Term)4 SpanTermQuery (org.apache.lucene.queries.spans.SpanTermQuery)4 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)4 Query (org.apache.lucene.search.Query)4 TermQuery (org.apache.lucene.search.TermQuery)4 Settings (org.opensearch.common.settings.Settings)4 SpanNearQuery (org.apache.lucene.queries.spans.SpanNearQuery)3 SpanOrQuery (org.apache.lucene.queries.spans.SpanOrQuery)3 SpanQuery (org.apache.lucene.queries.spans.SpanQuery)3 BooleanClause (org.apache.lucene.search.BooleanClause)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 BoostQuery (org.apache.lucene.search.BoostQuery)3 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 PhraseQuery (org.apache.lucene.search.PhraseQuery)3 PrefixQuery (org.apache.lucene.search.PrefixQuery)3 SynonymQuery (org.apache.lucene.search.SynonymQuery)3 MockSynonymAnalyzer (org.apache.lucene.tests.analysis.MockSynonymAnalyzer)2 Analyzer (org.apache.lucene.analysis.Analyzer)1