Search in sources :

Example 6 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project elasticsearch by elastic.

the class QueryStringQueryBuilderTests method testToQueryWithGraph.

public void testToQueryWithGraph() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    for (Operator op : Operator.values()) {
        BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
        MapperQueryParser queryParser = new MapperQueryParser(createShardContext());
        QueryParserSettings settings = new QueryParserSettings("");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.fuzziness(Fuzziness.AUTO);
        settings.analyzeWildcard(true);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        settings.forceAnalyzer(new MockSynonymAnalyzer());
        settings.forceQuoteAnalyzer(new MockSynonymAnalyzer());
        queryParser.reset(settings);
        // simple multi-term
        Query query = queryParser.parse("guinea pig");
        Query expectedQuery = new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), defaultOp).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // simple with additional tokens
        query = queryParser.parse("that guinea pig smells");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), defaultOp).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST)).add(new BooleanClause(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST)).build(), Occur.SHOULD).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), Occur.SHOULD).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), defaultOp).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // complex
        query = queryParser.parse("+that -(guinea pig) +smells");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), Occur.MUST).add(new BooleanQuery.Builder().add(new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), Occur.MUST).build(), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "cavy")), defaultOp).build(), Occur.MUST_NOT).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), Occur.MUST).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // no parent should cause guinea and pig to be treated as separate tokens
        query = queryParser.parse("+that -guinea pig +smells");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term(STRING_FIELD_NAME, "that")), BooleanClause.Occur.MUST).add(new TermQuery(new Term(STRING_FIELD_NAME, "guinea")), BooleanClause.Occur.MUST_NOT).add(new TermQuery(new Term(STRING_FIELD_NAME, "pig")), defaultOp).add(new TermQuery(new Term(STRING_FIELD_NAME, "smells")), BooleanClause.Occur.MUST).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // span query
        query = queryParser.parse("\"that guinea pig smells\"");
        expectedQuery = new BooleanQuery.Builder().add(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(STRING_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "smells"))).build(), Occur.SHOULD).setDisableCoord(true).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
        // span query with slop
        query = queryParser.parse("\"that guinea pig smells\"~2");
        expectedQuery = new BooleanQuery.Builder().add(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "that"))).addClause(new SpanOrQuery(new SpanNearQuery.Builder(STRING_FIELD_NAME, true).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "guinea"))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "pig"))).build(), new SpanTermQuery(new Term(STRING_FIELD_NAME, "cavy")))).addClause(new SpanTermQuery(new Term(STRING_FIELD_NAME, "smells"))).setSlop(2).build(), Occur.SHOULD).setDisableCoord(true).build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) MapperQueryParser(org.apache.lucene.queryparser.classic.MapperQueryParser) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Occur(org.apache.lucene.search.BooleanClause.Occur) QueryParserSettings(org.apache.lucene.queryparser.classic.QueryParserSettings) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 7 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project elasticsearch by elastic.

the class SimpleQueryParserTests method testAnalyzerWithGraph.

public void testAnalyzerWithGraph() {
    SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
    settings.analyzeWildcard(true);
    Map<String, Float> weights = new HashMap<>();
    weights.put("field1", 1.0f);
    SimpleQueryParser parser = new MockSimpleQueryParser(new MockSynonymAnalyzer(), weights, -1, settings);
    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("field1", "guinea")), defaultOp)).add(new BooleanClause(new TermQuery(new Term("field1", "pig")), defaultOp)).build();
        assertThat(query, equalTo(expectedQuery));
        // phrase will pick it up
        query = parser.parse("\"guinea pig\"");
        SpanTermQuery span1 = new SpanTermQuery(new Term("field1", "guinea"));
        SpanTermQuery span2 = new SpanTermQuery(new Term("field1", "pig"));
        expectedQuery = new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term("field1", "cavy")));
        assertThat(query, equalTo(expectedQuery));
        // phrase with slop
        query = parser.parse("big \"tiny guinea pig\"~2");
        expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("field1", "big")), defaultOp).add(new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("field1", "tiny")), new SpanOrQuery(new SpanNearQuery(new SpanQuery[] { span1, span2 }, 0, true), new SpanTermQuery(new Term("field1", "cavy"))) }, 2, true), defaultOp).build();
        assertThat(query, equalTo(expectedQuery));
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) HashMap(java.util.HashMap) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 8 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project lucene-solr by apache.

the class TestQueryParser method testOperatorsAndMultiWordSynonyms.

// TODO: Move to QueryParserTestBase once standard flexible parser gets this capability
public void testOperatorsAndMultiWordSynonyms() throws Exception {
    Analyzer a = new MockSynonymAnalyzer();
    boolean oldSplitOnWhitespace = splitOnWhitespace;
    splitOnWhitespace = false;
    // Operators should interrupt multiword analysis of adjacent words if they associate
    assertQueryEquals("+guinea pig", a, "+guinea pig");
    assertQueryEquals("-guinea pig", a, "-guinea pig");
    assertQueryEquals("!guinea pig", a, "-guinea pig");
    assertQueryEquals("guinea* pig", a, "guinea* pig");
    assertQueryEquals("guinea? pig", a, "guinea? pig");
    assertQueryEquals("guinea~2 pig", a, "guinea~2 pig");
    assertQueryEquals("guinea^2 pig", a, "(guinea)^2.0 pig");
    assertQueryEquals("guinea +pig", a, "guinea +pig");
    assertQueryEquals("guinea -pig", a, "guinea -pig");
    assertQueryEquals("guinea !pig", a, "guinea -pig");
    assertQueryEquals("guinea pig*", a, "guinea pig*");
    assertQueryEquals("guinea pig?", a, "guinea pig?");
    assertQueryEquals("guinea pig~2", a, "guinea pig~2");
    assertQueryEquals("guinea pig^2", a, "guinea (pig)^2.0");
    assertQueryEquals("field:guinea pig", a, "guinea pig");
    assertQueryEquals("guinea field:pig", a, "guinea pig");
    assertQueryEquals("NOT guinea pig", a, "-guinea pig");
    assertQueryEquals("guinea NOT pig", a, "guinea -pig");
    assertQueryEquals("guinea pig AND dogs", a, "guinea +pig +Synonym(dog dogs)");
    assertQueryEquals("dogs AND guinea pig", a, "+Synonym(dog dogs) +guinea pig");
    assertQueryEquals("guinea pig && dogs", a, "guinea +pig +Synonym(dog dogs)");
    assertQueryEquals("dogs && guinea pig", a, "+Synonym(dog dogs) +guinea pig");
    assertQueryEquals("guinea pig OR dogs", a, "guinea pig Synonym(dog dogs)");
    assertQueryEquals("dogs OR guinea pig", a, "Synonym(dog dogs) guinea pig");
    assertQueryEquals("guinea pig || dogs", a, "guinea pig Synonym(dog dogs)");
    assertQueryEquals("dogs || guinea pig", a, "Synonym(dog dogs) guinea pig");
    assertQueryEquals("\"guinea\" pig", a, "guinea pig");
    assertQueryEquals("guinea \"pig\"", a, "guinea pig");
    assertQueryEquals("(guinea) pig", a, "guinea pig");
    assertQueryEquals("guinea (pig)", a, "guinea pig");
    assertQueryEquals("/guinea/ pig", a, "/guinea/ pig");
    assertQueryEquals("guinea /pig/", a, "guinea /pig/");
    // Operators should not interrupt multiword analysis if not don't associate
    assertQueryEquals("(guinea pig)", a, "(+guinea +pig) cavy");
    assertQueryEquals("+(guinea pig)", a, "+((+guinea +pig) cavy)");
    assertQueryEquals("-(guinea pig)", a, "-((+guinea +pig) cavy)");
    assertQueryEquals("!(guinea pig)", a, "-((+guinea +pig) cavy)");
    assertQueryEquals("NOT (guinea pig)", a, "-((+guinea +pig) cavy)");
    assertQueryEquals("(guinea pig)^2", a, "((+guinea +pig) cavy)^2.0");
    assertQueryEquals("field:(guinea pig)", a, "(+guinea +pig) cavy");
    assertQueryEquals("+small guinea pig", a, "+small (+guinea +pig) cavy");
    assertQueryEquals("-small guinea pig", a, "-small (+guinea +pig) cavy");
    assertQueryEquals("!small guinea pig", a, "-small (+guinea +pig) cavy");
    assertQueryEquals("NOT small guinea pig", a, "-small (+guinea +pig) cavy");
    assertQueryEquals("small* guinea pig", a, "small* (+guinea +pig) cavy");
    assertQueryEquals("small? guinea pig", a, "small? (+guinea +pig) cavy");
    assertQueryEquals("\"small\" guinea pig", a, "small (+guinea +pig) cavy");
    assertQueryEquals("guinea pig +running", a, "(+guinea +pig) cavy +running");
    assertQueryEquals("guinea pig -running", a, "(+guinea +pig) cavy -running");
    assertQueryEquals("guinea pig !running", a, "(+guinea +pig) cavy -running");
    assertQueryEquals("guinea pig NOT running", a, "(+guinea +pig) cavy -running");
    assertQueryEquals("guinea pig running*", a, "(+guinea +pig) cavy running*");
    assertQueryEquals("guinea pig running?", a, "(+guinea +pig) cavy running?");
    assertQueryEquals("guinea pig \"running\"", a, "(+guinea +pig) cavy running");
    assertQueryEquals("\"guinea pig\"~2", a, "spanOr([spanNear([guinea, pig], 0, true), cavy])");
    assertQueryEquals("field:\"guinea pig\"", a, "spanOr([spanNear([guinea, pig], 0, true), cavy])");
    splitOnWhitespace = oldSplitOnWhitespace;
}
Also used : MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) MockBytesAnalyzer(org.apache.lucene.analysis.MockBytesAnalyzer) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer)

Aggregations

MockSynonymAnalyzer (org.apache.lucene.analysis.MockSynonymAnalyzer)8 Term (org.apache.lucene.index.Term)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 BoostQuery (org.apache.lucene.search.BoostQuery)3 Query (org.apache.lucene.search.Query)3 SynonymQuery (org.apache.lucene.search.SynonymQuery)3 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)3 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 MockBytesAnalyzer (org.apache.lucene.analysis.MockBytesAnalyzer)2 BooleanClause (org.apache.lucene.search.BooleanClause)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 SpanQuery (org.apache.lucene.search.spans.SpanQuery)2 HashMap (java.util.HashMap)1