Search in sources :

Example 11 with TermRangeQuery

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

the class CollationField method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
    String f = field.getName();
    BytesRef low = part1 == null ? null : getCollationKey(f, part1);
    BytesRef high = part2 == null ? null : getCollationKey(f, part2);
    if (!field.indexed() && field.hasDocValues()) {
        return SortedSetDocValuesField.newRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
    } else {
        return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
    }
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 12 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 13 with TermRangeQuery

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

the class RangeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query query = null;
    MappedFieldType mapper = context.fieldMapper(this.fieldName);
    if (mapper != null) {
        if (mapper instanceof DateFieldMapper.DateFieldType) {
            query = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, timeZone, getForceDateParser(), context);
        } else if (mapper instanceof RangeFieldMapper.RangeFieldType && mapper.typeName() == RangeFieldMapper.RangeType.DATE.name) {
            DateMathParser forcedDateParser = null;
            if (this.format != null) {
                forcedDateParser = new DateMathParser(this.format);
            }
            query = ((RangeFieldMapper.RangeFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, relation, timeZone, forcedDateParser, context);
        } else {
            if (timeZone != null) {
                throw new QueryShardException(context, "[range] time_zone can not be applied to non date field [" + fieldName + "]");
            }
            //LUCENE 4 UPGRADE Mapper#rangeQuery should use bytesref as well?
            query = mapper.rangeQuery(from, to, includeLower, includeUpper, context);
        }
    } else {
        if (timeZone != null) {
            throw new QueryShardException(context, "[range] time_zone can not be applied to non unmapped field [" + fieldName + "]");
        }
    }
    if (query == null) {
        query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper);
    }
    return query;
}
Also used : DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) Query(org.apache.lucene.search.Query) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) DateMathParser(org.elasticsearch.common.joda.DateMathParser)

Example 14 with TermRangeQuery

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

the class QueryStringQueryBuilderTests method testExpandedTerms.

public void testExpandedTerms() throws Exception {
    // Prefix
    Query query = new QueryStringQueryBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "aBc")), query);
    query = new QueryStringQueryBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "abc")), query);
    // Wildcard
    query = new QueryStringQueryBuilder("aBc*D").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new WildcardQuery(new Term(STRING_FIELD_NAME, "aBc*D")), query);
    query = new QueryStringQueryBuilder("aBc*D").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new WildcardQuery(new Term(STRING_FIELD_NAME, "abc*d")), query);
    // Fuzzy
    query = new QueryStringQueryBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
    assertEquals(new Term(STRING_FIELD_NAME, "aBc"), fuzzyQuery.getTerm());
    query = new QueryStringQueryBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    fuzzyQuery = (FuzzyQuery) query;
    assertEquals(new Term(STRING_FIELD_NAME, "abc"), fuzzyQuery.getTerm());
    // Range
    query = new QueryStringQueryBuilder("[aBc TO BcD]").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new TermRangeQuery(STRING_FIELD_NAME, new BytesRef("aBc"), new BytesRef("BcD"), true, true), query);
    query = new QueryStringQueryBuilder("[aBc TO BcD]").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new TermRangeQuery(STRING_FIELD_NAME, new BytesRef("abc"), new BytesRef("bcd"), true, true), query);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef)

Example 15 with TermRangeQuery

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

the class PercolatorFieldMapperTests method testExtractTermsAndRanges_failed.

public void testExtractTermsAndRanges_failed() throws Exception {
    addQueryMapping();
    TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true);
    DocumentMapper documentMapper = mapperService.documentMapper(typeName);
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(query, parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
    assertThat(document.getFields().size(), equalTo(1));
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_FAILED));
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

TermRangeQuery (org.apache.lucene.search.TermRangeQuery)29 BooleanQuery (org.apache.lucene.search.BooleanQuery)13 TermQuery (org.apache.lucene.search.TermQuery)11 IndexSearcher (org.apache.lucene.search.IndexSearcher)10 Query (org.apache.lucene.search.Query)10 BytesRef (org.apache.lucene.util.BytesRef)10 Term (org.apache.lucene.index.Term)8 Document (org.apache.lucene.document.Document)7 IndexReader (org.apache.lucene.index.IndexReader)5 BoostQuery (org.apache.lucene.search.BoostQuery)5 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)5 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)5 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)5 PrefixQuery (org.apache.lucene.search.PrefixQuery)5 ScoreDoc (org.apache.lucene.search.ScoreDoc)5 TextField (org.apache.lucene.document.TextField)4 IndexWriter (org.apache.lucene.index.IndexWriter)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3 PhraseQuery (org.apache.lucene.search.PhraseQuery)3 WildcardQuery (org.apache.lucene.search.WildcardQuery)3