Java 类org.apache.lucene.search.SynonymQuery 实例源码

项目:resource-query-parser    文件:QueryBuilder.java   
protected String toString(SynonymQuery synonymQuery, String field) {
    StringBuilder builder = new StringBuilder("(");
    List<Term> terms = synonymQuery.getTerms();

    for (int i = 0; i < terms.size(); i++) {
        if (i != 0) {
            builder.append(" ");
        }

        TermQuery termQuery = new TermQuery(terms.get(i));

        builder.append(toString(termQuery, field));
    }

    builder.append(")");

    return builder.toString();
}
项目:resource-query-parser    文件:TestQueryParser.java   
@Test
@Override
public void testNewFieldQuery() throws Exception {
    /** ordinary behavior, synonyms form uncoordinated boolean query */
    QueryParser dumb = new QueryParser("field", new Analyzer1());
    Query expanded = new SynonymQuery(new Term("field", "dogs"), new Term("field", "dog"));
    Assert.assertEquals(expanded, dumb.parse("\"dogs\""));
    /** even with the phrase operator the behavior is the same */
    Assert.assertEquals(expanded, dumb.parse("dogs"));

    /**
     * custom behavior, the synonyms are expanded, unless you use quote
     * operator
     */
    QueryParser smart = new SmartQueryParser();
    Assert.assertEquals(expanded, smart.parse("dogs"));

    Query unexpanded = new TermQuery(new Term("field", "dogs"));
    Assert.assertEquals(unexpanded, smart.parse("\"dogs\""));
}
项目:lucene-addons    文件:TestQueryParser.java   
@Override
public void testNewFieldQuery() throws Exception {
  /* ordinary behavior, synonyms form uncoordinated boolean query */
  QueryParser dumb = new QueryParser(FIELD,
      new Analyzer1());
  Query expanded = new SynonymQuery(new Term(FIELD, "dogs"), new Term(FIELD, "dog"));
  assertEquals(expanded, dumb.parse("\"dogs\""));
  /* even with the phrase operator the behavior is the same */
  assertEquals(expanded, dumb.parse("dogs"));

  /*
    custom behavior, the synonyms are expanded, unless you use quote operator
   */
  QueryParser smart = new SmartQueryParser();
  assertEquals(expanded, smart.parse("dogs"));

  Query unexpanded = new TermQuery(new Term(FIELD, "dogs"));
  assertEquals(unexpanded, smart.parse("\"dogs\""));
}
项目:elasticsearch_my    文件:SimpleQueryParserTests.java   
public void testAnalyzerWildcardWithSynonyms() {
    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 MockRepeatAnalyzer(), weights, -1, settings);

    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(new Term("field1", "first"),
                new Term("field1", "first")), defaultOp))
            .add(new BooleanQuery.Builder()
                .add(new BooleanClause(new SynonymQuery(new Term("field1", "foo"),
                    new Term("field1", "foo")), defaultOp))
                .add(new BooleanClause(new SynonymQuery(new Term("field1", "bar"),
                    new Term("field1", "bar")), defaultOp))
                .add(new BooleanQuery.Builder()
                    .add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .setDisableCoord(true)
                    .build(), defaultOp)
                .build(), defaultOp)
            .add(new BooleanClause(new SynonymQuery(new Term("field1", "last"),
                new Term("field1", "last")), defaultOp))
            .build();
        assertThat(query, equalTo(expectedQuery));
    }
}
项目:elasticsearch_my    文件:QueryStringQueryBuilderTests.java   
public void testToQueryWilcardQueryWithSynonyms() 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("first foo-bar-foobar* last");
        settings.defaultField(STRING_FIELD_NAME);
        settings.fieldsAndWeights(Collections.emptyMap());
        settings.analyzeWildcard(true);
        settings.fuzziness(Fuzziness.AUTO);
        settings.rewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
        settings.defaultOperator(op.toQueryParserOperator());
        settings.forceAnalyzer(new MockRepeatAnalyzer());
        queryParser.reset(settings);
        Query query = queryParser.parse("first foo-bar-foobar* last");

        Query expectedQuery = new BooleanQuery.Builder()
            .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "first"),
                new Term(STRING_FIELD_NAME, "first")), defaultOp))
            .add(new BooleanQuery.Builder()
                .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "foo"),
                    new Term(STRING_FIELD_NAME, "foo")), defaultOp))
                .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "bar"),
                    new Term(STRING_FIELD_NAME, "bar")), defaultOp))
                .add(new BooleanQuery.Builder()
                    .add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .add(new BooleanClause(new PrefixQuery(new Term(STRING_FIELD_NAME, "foobar")),
                        BooleanClause.Occur.SHOULD))
                    .setDisableCoord(true)
                    .build(), defaultOp)
                .build(), defaultOp)
            .add(new BooleanClause(new SynonymQuery(new Term(STRING_FIELD_NAME, "last"),
                new Term(STRING_FIELD_NAME, "last")), defaultOp))
            .build();
        assertThat(query, Matchers.equalTo(expectedQuery));
    }
}
项目:elasticsearch_my    文件:MultiMatchQueryTests.java   
public void testMultiMatchCrossFieldsWithSynonyms() throws IOException {
    QueryShardContext queryShardContext = indexService.newQueryShardContext(
        randomInt(20), null, () -> { throw new UnsupportedOperationException(); });

    // check that synonym query is used for a single field
    Query parsedQuery =
        multiMatchQuery("quick").field("name.first")
            .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
    Term[] terms = new Term[2];
    terms[0] = new Term("name.first", "quick");
    terms[1] = new Term("name.first", "fast");
    Query expectedQuery = new SynonymQuery(terms);
    assertThat(parsedQuery, equalTo(expectedQuery));

    // check that blended term query is used for multiple fields
    parsedQuery =
        multiMatchQuery("quick").field("name.first").field("name.last")
            .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
    terms = new Term[4];
    terms[0] = new Term("name.first", "quick");
    terms[1] = new Term("name.first", "fast");
    terms[2] = new Term("name.last", "quick");
    terms[3] = new Term("name.last", "fast");
    float[] boosts = new float[4];
    Arrays.fill(boosts, 1.0f);
    expectedQuery = BlendedTermQuery.dismaxBlendedQuery(terms, boosts, 1.0f);
    assertThat(parsedQuery, equalTo(expectedQuery));

}
项目:resource-query-parser    文件:QueryBuilder.java   
public String toString(Query query, String field) {
    StringBuilder buffer = new StringBuilder();

    if (query instanceof TermQuery) {
        buffer.append(toString((TermQuery) query, field));
    } else if (query instanceof BooleanQuery) {
        buffer.append(toString((BooleanQuery) query, field));
    } else if (query instanceof WildcardQuery) {
        buffer.append(toString((WildcardQuery) query, field));
    } else if (query instanceof PhraseQuery) {
        buffer.append(toString((PhraseQuery) query, field));
    } else if (query instanceof PrefixQuery) {
        buffer.append(toString((PrefixQuery) query, field));
    } else if (query instanceof MultiPhraseQuery) {
        buffer.append(toString((MultiPhraseQuery) query, field));
    } else if (query instanceof FuzzyQuery) {
        buffer.append(toString((FuzzyQuery) query, field));
    } else if (query instanceof RegexpQuery) {
        buffer.append(toString((RegexpQuery) query, field));
    } else if (query instanceof TermRangeQuery) {
        buffer.append(toString((TermRangeQuery) query, field));
    } else if (query instanceof ConstantScoreQuery) {
        buffer.append(toString((ConstantScoreQuery) query, field));
    } else if (query instanceof DisjunctionMaxQuery) {
        buffer.append(toString((DisjunctionMaxQuery) query, field));
    } else if (query instanceof MatchAllDocsQuery) {
        buffer.append(toString((MatchAllDocsQuery) query, field));
    } else if (query instanceof SynonymQuery) {
        buffer.append(toString((SynonymQuery) query, field));
    } else {
        buffer.append(query.toString(field));
    }

    return buffer.toString();
}
项目:elasticsearch_my    文件:QueryAnalyzer.java   
static Function<Query, Result> synonymQuery() {
    return query -> {
        Set<Term> terms = new HashSet<>(((SynonymQuery) query).getTerms());
        return new Result(true, terms);
    };
}
项目:elasticsearch_my    文件:CustomFieldQuery.java   
@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
        flatten(sourceQuery, reader, flatQueries, boost);
    } else if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().length], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ESToParentBlockJoinQuery) {
        ESToParentBlockJoinQuery blockJoinQuery = (ESToParentBlockJoinQuery) sourceQuery;
        flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof BoostingQuery) {
        BoostingQuery boostingQuery = (BoostingQuery) sourceQuery;
        //flatten positive query with query boost
        flatten(boostingQuery.getMatch(), reader, flatQueries, boost);
        //flatten negative query with negative boost
        flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost());
    } else if (sourceQuery instanceof SynonymQuery) {
        // SynonymQuery should be handled by the parent class directly.
        // This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
项目:elasticsearch_my    文件:MatchQuery.java   
protected Query blendTermsQuery(Term[] terms, MappedFieldType fieldType) {
    return new SynonymQuery(terms);
}
项目:resource-query-parser    文件:QueryBuilder.java   
/**
 * Builds a new SynonymQuery instance.
 * <p>
 * This is intended for subclasses that wish to customize the generated
 * queries.
 * 
 * @return new Query instance
 */
protected Query newSynonymQuery(Term terms[]) {
    return new SynonymQuery(terms);
}