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