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));
}
}
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));
}
}
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;
}
Aggregations