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);
}
}
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();
}
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;
}
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);
}
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));
}
Aggregations