Search in sources :

Example 1 with ExtendedCommonTermsQuery

use of org.apache.lucene.queries.ExtendedCommonTermsQuery in project elasticsearch by elastic.

the class CommonTermsQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    String field;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        field = fieldType.name();
    } else {
        field = fieldName;
    }
    Analyzer analyzerObj;
    if (analyzer == null) {
        if (fieldType != null) {
            analyzerObj = context.getSearchAnalyzer(fieldType);
        } else {
            analyzerObj = context.getMapperService().searchAnalyzer();
        }
    } else {
        analyzerObj = context.getMapperService().getIndexAnalyzers().get(analyzer);
        if (analyzerObj == null) {
            throw new QueryShardException(context, "[common] analyzer [" + analyzer + "] not found");
        }
    }
    Occur highFreqOccur = highFreqOperator.toBooleanClauseOccur();
    Occur lowFreqOccur = lowFreqOperator.toBooleanClauseOccur();
    ExtendedCommonTermsQuery commonsQuery = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, cutoffFrequency, disableCoord, fieldType);
    return parseQueryString(commonsQuery, text, field, analyzerObj, lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch);
}
Also used : ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Occur(org.apache.lucene.search.BooleanClause.Occur) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 2 with ExtendedCommonTermsQuery

use of org.apache.lucene.queries.ExtendedCommonTermsQuery in project elasticsearch by elastic.

the class CommonTermsQueryBuilderTests method testCommonTermsQuery2.

public void testCommonTermsQuery2() throws IOException {
    String query = copyToStringFromClasspath("/org/elasticsearch/index/query/commonTerms-query2.json");
    Query parsedQuery = parseQuery(query).toQuery(createShardContext());
    assertThat(parsedQuery, instanceOf(ExtendedCommonTermsQuery.class));
    ExtendedCommonTermsQuery ectQuery = (ExtendedCommonTermsQuery) parsedQuery;
    assertThat(ectQuery.getHighFreqMinimumNumberShouldMatchSpec(), equalTo("50%"));
    assertThat(ectQuery.getLowFreqMinimumNumberShouldMatchSpec(), equalTo("5<20%"));
}
Also used : Query(org.apache.lucene.search.Query) QueryBuilders.commonTermsQuery(org.elasticsearch.index.query.QueryBuilders.commonTermsQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery)

Example 3 with ExtendedCommonTermsQuery

use of org.apache.lucene.queries.ExtendedCommonTermsQuery in project elasticsearch by elastic.

the class CommonTermsQueryBuilderTests method testCommonTermsQuery3.

public void testCommonTermsQuery3() throws IOException {
    String query = copyToStringFromClasspath("/org/elasticsearch/index/query/commonTerms-query3.json");
    Query parsedQuery = parseQuery(query).toQuery(createShardContext());
    assertThat(parsedQuery, instanceOf(ExtendedCommonTermsQuery.class));
    ExtendedCommonTermsQuery ectQuery = (ExtendedCommonTermsQuery) parsedQuery;
    assertThat(ectQuery.getHighFreqMinimumNumberShouldMatchSpec(), nullValue());
    assertThat(ectQuery.getLowFreqMinimumNumberShouldMatchSpec(), equalTo("2"));
}
Also used : Query(org.apache.lucene.search.Query) QueryBuilders.commonTermsQuery(org.elasticsearch.index.query.QueryBuilders.commonTermsQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery)

Example 4 with ExtendedCommonTermsQuery

use of org.apache.lucene.queries.ExtendedCommonTermsQuery in project elasticsearch by elastic.

the class CommonTermsQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(CommonTermsQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, instanceOf(ExtendedCommonTermsQuery.class));
    ExtendedCommonTermsQuery extendedCommonTermsQuery = (ExtendedCommonTermsQuery) query;
    assertThat(extendedCommonTermsQuery.getHighFreqMinimumNumberShouldMatchSpec(), equalTo(queryBuilder.highFreqMinimumShouldMatch()));
    assertThat(extendedCommonTermsQuery.getLowFreqMinimumNumberShouldMatchSpec(), equalTo(queryBuilder.lowFreqMinimumShouldMatch()));
}
Also used : ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery)

Example 5 with ExtendedCommonTermsQuery

use of org.apache.lucene.queries.ExtendedCommonTermsQuery in project elasticsearch by elastic.

the class MatchQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query, SearchContext searchContext) throws IOException {
    assertThat(query, notNullValue());
    if (query instanceof MatchAllDocsQuery) {
        assertThat(queryBuilder.zeroTermsQuery(), equalTo(ZeroTermsQuery.ALL));
        return;
    }
    switch(queryBuilder.type()) {
        case BOOLEAN:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(MatchNoDocsQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
        case PHRASE:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(PhraseQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
        case PHRASE_PREFIX:
            assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
            break;
    }
    QueryShardContext context = searchContext.getQueryShardContext();
    MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
    if (query instanceof TermQuery && fieldType != null) {
        String queryValue = queryBuilder.value().toString();
        if (queryBuilder.analyzer() == null || queryBuilder.analyzer().equals("simple")) {
            queryValue = queryValue.toLowerCase(Locale.ROOT);
        }
        Query expectedTermQuery = fieldType.termQuery(queryValue, context);
        assertEquals(expectedTermQuery, query);
    }
    if (query instanceof BooleanQuery) {
        BooleanQuery bq = (BooleanQuery) query;
        if (queryBuilder.minimumShouldMatch() != null) {
            // calculate expected minimumShouldMatch value
            int optionalClauses = 0;
            for (BooleanClause c : bq.clauses()) {
                if (c.getOccur() == BooleanClause.Occur.SHOULD) {
                    optionalClauses++;
                }
            }
            int msm = Queries.calculateMinShouldMatch(optionalClauses, queryBuilder.minimumShouldMatch());
            assertThat(bq.getMinimumNumberShouldMatch(), equalTo(msm));
        }
        if (queryBuilder.analyzer() == null && queryBuilder.value().toString().length() > 0) {
            assertEquals(bq.clauses().size(), queryBuilder.value().toString().split(" ").length);
        }
    }
    if (query instanceof ExtendedCommonTermsQuery) {
        assertTrue(queryBuilder.cutoffFrequency() != null);
        ExtendedCommonTermsQuery ectq = (ExtendedCommonTermsQuery) query;
        assertEquals(queryBuilder.cutoffFrequency(), ectq.getMaxTermFrequency(), Float.MIN_VALUE);
    }
    if (query instanceof FuzzyQuery) {
        assertTrue(queryBuilder.fuzziness() != null);
        FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
        // depending on analyzer being set or not we can have term lowercased along the way, so to simplify test we just
        // compare lowercased terms here
        String originalTermLc = queryBuilder.value().toString().toLowerCase(Locale.ROOT);
        String actualTermLc = fuzzyQuery.getTerm().text().toLowerCase(Locale.ROOT);
        Matcher<String> termLcMatcher = equalTo(originalTermLc);
        if ("false".equals(originalTermLc) || "true".equals(originalTermLc)) {
            // Booleans become t/f when querying a boolean field
            termLcMatcher = either(termLcMatcher).or(equalTo(originalTermLc.substring(0, 1)));
        }
        assertThat(actualTermLc, termLcMatcher);
        assertThat(queryBuilder.prefixLength(), equalTo(fuzzyQuery.getPrefixLength()));
        assertThat(queryBuilder.fuzzyTranspositions(), equalTo(fuzzyQuery.getTranspositions()));
    }
    if (query instanceof PointRangeQuery) {
    // TODO
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) Query(org.apache.lucene.search.Query) ZeroTermsQuery(org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanClause(org.apache.lucene.search.BooleanClause) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType)

Aggregations

ExtendedCommonTermsQuery (org.apache.lucene.queries.ExtendedCommonTermsQuery)7 Query (org.apache.lucene.search.Query)5 QueryBuilders.commonTermsQuery (org.elasticsearch.index.query.QueryBuilders.commonTermsQuery)4 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 Analyzer (org.apache.lucene.analysis.Analyzer)1 BooleanClause (org.apache.lucene.search.BooleanClause)1 Occur (org.apache.lucene.search.BooleanClause.Occur)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 PointRangeQuery (org.apache.lucene.search.PointRangeQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 MultiPhrasePrefixQuery (org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)1 MatchQuery (org.elasticsearch.index.search.MatchQuery)1 ZeroTermsQuery (org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1