Search in sources :

Example 1 with QueryRewriteContext

use of org.elasticsearch.index.query.QueryRewriteContext in project elasticsearch by elastic.

the class DateFieldTypeTests method testIsFieldWithinQueryEmptyReader.

public void testIsFieldWithinQueryEmptyReader() throws IOException {
    QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    IndexReader reader = new MultiReader();
    DateFieldType ft = new DateFieldType();
    ft.setName("my_date");
    assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", randomBoolean(), randomBoolean(), null, null, context));
}
Also used : DateFieldType(org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType) MultiReader(org.apache.lucene.index.MultiReader) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) IndexReader(org.apache.lucene.index.IndexReader)

Example 2 with QueryRewriteContext

use of org.elasticsearch.index.query.QueryRewriteContext in project elasticsearch by elastic.

the class DateFieldTypeTests method doTestIsFieldWithinQuery.

private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader, DateTimeZone zone, DateMathParser alternateFormat) throws IOException {
    QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", randomBoolean(), randomBoolean(), null, null, context));
    assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2016-01-02", "2016-06-20", randomBoolean(), randomBoolean(), null, null, context));
    assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2016-01-02", "2016-02-12", randomBoolean(), randomBoolean(), null, null, context));
    assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2014-01-02", "2015-02-12", randomBoolean(), randomBoolean(), null, null, context));
    assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2016-05-11", "2016-08-30", randomBoolean(), randomBoolean(), null, null, context));
    assertEquals(Relation.WITHIN, ft.isFieldWithinQuery(reader, "2015-09-25", "2016-05-29", randomBoolean(), randomBoolean(), null, null, context));
    assertEquals(Relation.WITHIN, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", true, true, null, null, context));
    assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", false, false, null, null, context));
    assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", false, true, null, null, context));
    assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", true, false, null, null, context));
}
Also used : QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext)

Example 3 with QueryRewriteContext

use of org.elasticsearch.index.query.QueryRewriteContext in project elasticsearch by elastic.

the class AliasFilter method reparseFilter.

private QueryBuilder reparseFilter(QueryRewriteContext context) {
    if (reparseAliases) {
        // we are processing a filter received from a 5.0 node - we need to reparse this on the executing node
        final IndexMetaData indexMetaData = context.getIndexSettings().getIndexMetaData();
        /* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch
             * of dependencies we pass in a function that can perform the parsing. */
        CheckedFunction<byte[], QueryBuilder, IOException> filterParser = bytes -> {
            try (XContentParser parser = XContentFactory.xContent(bytes).createParser(context.getXContentRegistry(), bytes)) {
                return context.newParseContext(parser).parseInnerQueryBuilder();
            }
        };
        return ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliases);
    }
    return filter;
}
Also used : XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) IOException(java.io.IOException) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Objects(java.util.Objects) CheckedFunction(org.elasticsearch.common.CheckedFunction) Version(org.elasticsearch.Version) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Writeable(org.elasticsearch.common.io.stream.Writeable) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 4 with QueryRewriteContext

use of org.elasticsearch.index.query.QueryRewriteContext in project elasticsearch by elastic.

the class DateFieldTypeTests method testIsFieldWithinQuery.

public void testIsFieldWithinQuery() throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-12").getMillis();
    long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime("2016-04-03").getMillis();
    Document doc = new Document();
    LongPoint field = new LongPoint("my_date", instant1);
    doc.add(field);
    w.addDocument(doc);
    field.setLongValue(instant2);
    w.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(w);
    DateFieldType ft = new DateFieldType();
    ft.setName("my_date");
    DateMathParser alternateFormat = new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER);
    doTestIsFieldWithinQuery(ft, reader, null, null);
    doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat);
    // Fields with no value indexed.
    DateFieldType ft2 = new DateFieldType();
    ft2.setName("my_date2");
    QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context));
    IOUtils.close(reader, w, dir);
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) DateFieldType(org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) LongPoint(org.apache.lucene.document.LongPoint) DateMathParser(org.elasticsearch.common.joda.DateMathParser) Document(org.elasticsearch.index.mapper.ParseContext.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

QueryRewriteContext (org.elasticsearch.index.query.QueryRewriteContext)4 DateFieldType (org.elasticsearch.index.mapper.DateFieldMapper.DateFieldType)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Objects (java.util.Objects)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 MultiReader (org.apache.lucene.index.MultiReader)1 Directory (org.apache.lucene.store.Directory)1 Version (org.elasticsearch.Version)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 CheckedFunction (org.elasticsearch.common.CheckedFunction)1 Strings (org.elasticsearch.common.Strings)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)1 Writeable (org.elasticsearch.common.io.stream.Writeable)1 DateMathParser (org.elasticsearch.common.joda.DateMathParser)1