Search in sources :

Example 1 with PointRangeQuery

use of org.apache.lucene.search.PointRangeQuery 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)

Example 2 with PointRangeQuery

use of org.apache.lucene.search.PointRangeQuery in project lucene-solr by apache.

the class TestPointFields method testIndexOrDocValuesQuery.

@Test
public void testIndexOrDocValuesQuery() throws Exception {
    String[] fieldTypeNames = new String[] { "_p_i", "_p_l", "_p_d", "_p_f" };
    FieldType[] fieldTypes = new FieldType[] { new IntPointField(), new LongPointField(), new DoublePointField(), new FloatPointField() };
    assert fieldTypeNames.length == fieldTypes.length;
    for (int i = 0; i < fieldTypeNames.length; i++) {
        SchemaField fieldIndexed = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i]);
        SchemaField fieldIndexedAndDv = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i] + "_dv");
        SchemaField fieldIndexedMv = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i] + "_mv");
        SchemaField fieldIndexedAndDvMv = h.getCore().getLatestSchema().getField("foo_" + fieldTypeNames[i] + "_mv_dv");
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexed, "0", "10", true, true) instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexedAndDv, "0", "10", true, true) instanceof IndexOrDocValuesQuery);
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexedMv, "0", "10", true, true) instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getRangeQuery(null, fieldIndexedAndDvMv, "0", "10", true, true) instanceof IndexOrDocValuesQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexed, "0") instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexedAndDv, "0") instanceof IndexOrDocValuesQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexedMv, "0") instanceof PointRangeQuery);
        assertTrue(fieldTypes[i].getFieldQuery(null, fieldIndexedAndDvMv, "0") instanceof IndexOrDocValuesQuery);
    }
}
Also used : PointRangeQuery(org.apache.lucene.search.PointRangeQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) Test(org.junit.Test)

Aggregations

IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)2 PointRangeQuery (org.apache.lucene.search.PointRangeQuery)2 DoublePoint (org.apache.lucene.document.DoublePoint)1 FloatPoint (org.apache.lucene.document.FloatPoint)1 IntPoint (org.apache.lucene.document.IntPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 ExtendedCommonTermsQuery (org.apache.lucene.queries.ExtendedCommonTermsQuery)1 BooleanClause (org.apache.lucene.search.BooleanClause)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 Query (org.apache.lucene.search.Query)1 TermQuery (org.apache.lucene.search.TermQuery)1 MultiPhrasePrefixQuery (org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1 MatchQuery (org.elasticsearch.index.search.MatchQuery)1 ZeroTermsQuery (org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery)1