Search in sources :

Example 1 with TermRangeQuery

use of org.apache.lucene.search.TermRangeQuery in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_unsupportedQueryInBoolQueryWithMustClauses.

public void testExtractQueryMetadata_unsupportedQueryInBoolQueryWithMustClauses() {
    TermRangeQuery unsupportedQuery = new TermRangeQuery("_field", null, null, true, false);
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.MUST);
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    BooleanQuery bq1 = builder.build();
    Result result = analyze(bq1);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, termQuery1.getTerm());
    TermQuery termQuery2 = new TermQuery(new Term("_field", "_longer_term"));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.MUST);
    builder.add(termQuery2, BooleanClause.Occur.MUST);
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    bq1 = builder.build();
    result = analyze(bq1);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, termQuery2.getTerm());
    builder = new BooleanQuery.Builder();
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    BooleanQuery bq2 = builder.build();
    UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq2));
    assertThat(e.getUnsupportedQuery(), sameInstance(unsupportedQuery));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) UnsupportedQueryException(org.elasticsearch.percolator.QueryAnalyzer.UnsupportedQueryException) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 2 with TermRangeQuery

use of org.apache.lucene.search.TermRangeQuery in project elasticsearch by elastic.

the class RangeQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false && queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false)) {
        assertThat(query, instanceOf(TermRangeQuery.class));
        TermRangeQuery termRangeQuery = (TermRangeQuery) query;
        assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName()));
        assertThat(termRangeQuery.getLowerTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.from())));
        assertThat(termRangeQuery.getUpperTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.to())));
        assertThat(termRangeQuery.includesLower(), equalTo(queryBuilder.includeLower()));
        assertThat(termRangeQuery.includesUpper(), equalTo(queryBuilder.includeUpper()));
    } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) {
        assertThat(query, instanceOf(IndexOrDocValuesQuery.class));
        query = ((IndexOrDocValuesQuery) query).getIndexQuery();
        assertThat(query, instanceOf(PointRangeQuery.class));
        MapperService mapperService = context.getQueryShardContext().getMapperService();
        MappedFieldType mappedFieldType = mapperService.fullName(DATE_FIELD_NAME);
        final Long fromInMillis;
        final Long toInMillis;
        // we have to normalize the incoming value into milliseconds since it could be literally anything
        if (mappedFieldType instanceof DateFieldMapper.DateFieldType) {
            fromInMillis = queryBuilder.from() == null ? null : ((DateFieldMapper.DateFieldType) mappedFieldType).parseToMilliseconds(queryBuilder.from(), queryBuilder.includeLower(), queryBuilder.getDateTimeZone(), queryBuilder.getForceDateParser(), context.getQueryShardContext());
            toInMillis = queryBuilder.to() == null ? null : ((DateFieldMapper.DateFieldType) mappedFieldType).parseToMilliseconds(queryBuilder.to(), queryBuilder.includeUpper(), queryBuilder.getDateTimeZone(), queryBuilder.getForceDateParser(), context.getQueryShardContext());
        } else {
            fromInMillis = toInMillis = null;
            fail("unexpected mapped field type: [" + mappedFieldType.getClass() + "] " + mappedFieldType.toString());
        }
        Long min = fromInMillis;
        Long max = toInMillis;
        long minLong, maxLong;
        if (min == null) {
            minLong = Long.MIN_VALUE;
        } else {
            minLong = min.longValue();
            if (queryBuilder.includeLower() == false && minLong != Long.MAX_VALUE) {
                minLong++;
            }
        }
        if (max == null) {
            maxLong = Long.MAX_VALUE;
        } else {
            maxLong = max.longValue();
            if (queryBuilder.includeUpper() == false && maxLong != Long.MIN_VALUE) {
                maxLong--;
            }
        }
        assertEquals(LongPoint.newRangeQuery(DATE_FIELD_NAME, minLong, maxLong), query);
    } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) {
        assertThat(query, instanceOf(IndexOrDocValuesQuery.class));
        query = ((IndexOrDocValuesQuery) query).getIndexQuery();
        assertThat(query, instanceOf(PointRangeQuery.class));
        Integer min = (Integer) queryBuilder.from();
        Integer max = (Integer) queryBuilder.to();
        int minInt, maxInt;
        if (min == null) {
            minInt = Integer.MIN_VALUE;
        } else {
            minInt = min.intValue();
            if (queryBuilder.includeLower() == false && minInt != Integer.MAX_VALUE) {
                minInt++;
            }
        }
        if (max == null) {
            maxInt = Integer.MAX_VALUE;
        } else {
            maxInt = max.intValue();
            if (queryBuilder.includeUpper() == false && maxInt != Integer.MIN_VALUE) {
                maxInt--;
            }
        }
    } else if (queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) || queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME)) {
    // todo can't check RangeFieldQuery because its currently package private (this will change)
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MapperService(org.elasticsearch.index.mapper.MapperService) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint)

Example 3 with TermRangeQuery

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

the class CollationTestBase method testFarsiTermRangeQuery.

public void testFarsiTermRangeQuery(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd) throws Exception {
    Directory farsiIndex = newDirectory();
    IndexWriter writer = new IndexWriter(farsiIndex, new IndexWriterConfig(analyzer));
    Document doc = new Document();
    doc.add(new TextField("content", "ساب", Field.Store.YES));
    doc.add(new StringField("body", "body", Field.Store.YES));
    writer.addDocument(doc);
    writer.close();
    IndexReader reader = DirectoryReader.open(farsiIndex);
    IndexSearcher search = newSearcher(reader);
    // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
    // orders the U+0698 character before the U+0633 character, so the single
    // index Term below should NOT be returned by a TermRangeQuery
    // with a Farsi Collator (or an Arabic one for the case when Farsi is 
    // not supported).
    Query csrq = new TermRangeQuery("content", firstBeg, firstEnd, true, true);
    ScoreDoc[] result = search.search(csrq, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);
    csrq = new TermRangeQuery("content", secondBeg, secondEnd, true, true);
    result = search.search(csrq, 1000).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);
    reader.close();
    farsiIndex.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 4 with TermRangeQuery

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

the class TermRangeQueryNodeBuilder method build.

@Override
public TermRangeQuery build(QueryNode queryNode) throws QueryNodeException {
    TermRangeQueryNode rangeNode = (TermRangeQueryNode) queryNode;
    FieldQueryNode upper = rangeNode.getUpperBound();
    FieldQueryNode lower = rangeNode.getLowerBound();
    String field = StringUtils.toString(rangeNode.getField());
    String lowerText = lower.getTextAsString();
    String upperText = upper.getTextAsString();
    if (lowerText.length() == 0) {
        lowerText = null;
    }
    if (upperText.length() == 0) {
        upperText = null;
    }
    TermRangeQuery rangeQuery = TermRangeQuery.newStringRange(field, lowerText, upperText, rangeNode.isLowerInclusive(), rangeNode.isUpperInclusive());
    MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod) queryNode.getTag(MultiTermRewriteMethodProcessor.TAG_ID);
    if (method != null) {
        rangeQuery.setRewriteMethod(method);
    }
    return rangeQuery;
}
Also used : FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)

Example 5 with TermRangeQuery

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

the class FieldQueryTest method testRangeQuery.

public void testRangeQuery() throws Exception {
    makeIndexStrMV();
    defgMultiTermQueryTest(new TermRangeQuery(F, new BytesRef("d"), new BytesRef("e"), true, true));
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

TermRangeQuery (org.apache.lucene.search.TermRangeQuery)38 BooleanQuery (org.apache.lucene.search.BooleanQuery)18 BytesRef (org.apache.lucene.util.BytesRef)16 TermQuery (org.apache.lucene.search.TermQuery)15 Query (org.apache.lucene.search.Query)13 Term (org.apache.lucene.index.Term)11 IndexSearcher (org.apache.lucene.search.IndexSearcher)10 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)8 Document (org.apache.lucene.document.Document)7 BoostQuery (org.apache.lucene.search.BoostQuery)6 IndexReader (org.apache.lucene.index.IndexReader)5 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)5 PhraseQuery (org.apache.lucene.search.PhraseQuery)5 ScoreDoc (org.apache.lucene.search.ScoreDoc)5 TextField (org.apache.lucene.document.TextField)4 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)4 PrefixQuery (org.apache.lucene.search.PrefixQuery)4 RegexpQuery (org.apache.lucene.search.RegexpQuery)4 Date (java.util.Date)3 Map (java.util.Map)3