Search in sources :

Example 26 with LongPoint

use of org.apache.lucene.document.LongPoint in project elasticsearch by elastic.

the class SeqNoFieldMapper method postParse.

@Override
public void postParse(ParseContext context) throws IOException {
    // for efficiency.
    for (int i = 1; i < context.docs().size(); i++) {
        final Document doc = context.docs().get(i);
        doc.add(new LongPoint(NAME, 1));
        doc.add(new SortedNumericDocValuesField(NAME, 1L));
        doc.add(new NumericDocValuesField(PRIMARY_TERM_NAME, 0L));
    }
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) LongPoint(org.apache.lucene.document.LongPoint) Document(org.elasticsearch.index.mapper.ParseContext.Document) LongPoint(org.apache.lucene.document.LongPoint)

Example 27 with LongPoint

use of org.apache.lucene.document.LongPoint in project elasticsearch by elastic.

the class ScaledFloatFieldTypeTests method testRangeQuery.

public void testRangeQuery() throws IOException {
    // make sure the accuracy loss of scaled floats only occurs at index time
    // this test checks that searching scaled floats yields the same results as
    // searching doubles that are rounded to the closest half float
    ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
    ft.setName("scaled_float");
    ft.setScalingFactor(0.1 + randomDouble() * 100);
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    final int numDocs = 1000;
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        double value = (randomDouble() * 2 - 1) * 10000;
        long scaledValue = Math.round(value * ft.getScalingFactor());
        double rounded = scaledValue / ft.getScalingFactor();
        doc.add(new LongPoint("scaled_float", scaledValue));
        doc.add(new DoublePoint("double", rounded));
        w.addDocument(doc);
    }
    final DirectoryReader reader = DirectoryReader.open(w);
    w.close();
    IndexSearcher searcher = newSearcher(reader);
    final int numQueries = 1000;
    for (int i = 0; i < numQueries; ++i) {
        Double l = randomBoolean() ? null : (randomDouble() * 2 - 1) * 10000;
        Double u = randomBoolean() ? null : (randomDouble() * 2 - 1) * 10000;
        boolean includeLower = randomBoolean();
        boolean includeUpper = randomBoolean();
        Query doubleQ = NumberFieldMapper.NumberType.DOUBLE.rangeQuery("double", l, u, includeLower, includeUpper, false);
        Query scaledFloatQ = ft.rangeQuery(l, u, includeLower, includeUpper, null);
        assertEquals(searcher.count(doubleQ), searcher.count(scaledFloatQ));
    }
    IOUtils.close(reader, dir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IndexWriter(org.apache.lucene.index.IndexWriter) DoublePoint(org.apache.lucene.document.DoublePoint) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 28 with LongPoint

use of org.apache.lucene.document.LongPoint in project elasticsearch by elastic.

the class DateHistogramAggregatorTests method executeTestCase.

private void executeTestCase(boolean reduced, Query query, List<String> dataset, Consumer<DateHistogramAggregationBuilder> configure, Consumer<Histogram> verify) throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            for (String date : dataset) {
                if (frequently()) {
                    indexWriter.commit();
                }
                long instant = asLong(date);
                document.add(new SortedNumericDocValuesField(DATE_FIELD, instant));
                document.add(new LongPoint(INSTANT_FIELD, instant));
                indexWriter.addDocument(document);
                document.clear();
            }
        }
        try (IndexReader indexReader = DirectoryReader.open(directory)) {
            IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
            DateHistogramAggregationBuilder aggregationBuilder = new DateHistogramAggregationBuilder("_name");
            if (configure != null) {
                configure.accept(aggregationBuilder);
            }
            DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name");
            DateFieldMapper.DateFieldType fieldType = builder.fieldType();
            fieldType.setHasDocValues(true);
            fieldType.setName(aggregationBuilder.field());
            InternalDateHistogram histogram;
            if (reduced) {
                histogram = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType);
            } else {
                histogram = search(indexSearcher, query, aggregationBuilder, fieldType);
            }
            verify.accept(histogram);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 29 with LongPoint

use of org.apache.lucene.document.LongPoint in project elasticsearch by elastic.

the class CandidateQueryTests method testDuelSpecificQueries.

public void testDuelSpecificQueries() throws Exception {
    List<ParseContext.Document> documents = new ArrayList<>();
    CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 128);
    commonTermsQuery.add(new Term("field", "quick"));
    commonTermsQuery.add(new Term("field", "brown"));
    commonTermsQuery.add(new Term("field", "fox"));
    addQuery(commonTermsQuery, documents);
    BlendedTermQuery blendedTermQuery = BlendedTermQuery.booleanBlendedQuery(new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") }, false);
    addQuery(blendedTermQuery, documents);
    SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "quick"))).addClause(new SpanTermQuery(new Term("field", "brown"))).addClause(new SpanTermQuery(new Term("field", "fox"))).build();
    addQuery(spanNearQuery, documents);
    SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "the"))).addClause(new SpanTermQuery(new Term("field", "lazy"))).addClause(new SpanTermQuery(new Term("field", "doc"))).build();
    SpanOrQuery spanOrQuery = new SpanOrQuery(spanNearQuery, spanNearQuery2);
    addQuery(spanOrQuery, documents);
    SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery);
    addQuery(spanNotQuery, documents);
    long lowerLong = randomIntBetween(0, 256);
    long upperLong = lowerLong + randomIntBetween(0, 32);
    addQuery(LongPoint.newRangeQuery("long_field", lowerLong, upperLong), documents);
    indexWriter.addDocuments(documents);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    // Disable query cache, because ControlQuery cannot be cached...
    shardSearcher.setQueryCache(null);
    Document document = new Document();
    document.add(new TextField("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO));
    long randomLong = randomIntBetween((int) lowerLong, (int) upperLong);
    document.add(new LongPoint("long_field", randomLong));
    MemoryIndex memoryIndex = MemoryIndex.fromDocument(document, new WhitespaceAnalyzer());
    duelRun(queryStore, memoryIndex, shardSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) ArrayList(java.util.ArrayList) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TextField(org.apache.lucene.document.TextField) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 30 with LongPoint

use of org.apache.lucene.document.LongPoint 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

LongPoint (org.apache.lucene.document.LongPoint)49 Document (org.apache.lucene.document.Document)41 Directory (org.apache.lucene.store.Directory)34 IndexReader (org.apache.lucene.index.IndexReader)26 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)26 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)20 DoublePoint (org.apache.lucene.document.DoublePoint)19 FloatPoint (org.apache.lucene.document.FloatPoint)16 IntPoint (org.apache.lucene.document.IntPoint)16 IndexSearcher (org.apache.lucene.search.IndexSearcher)16 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)15 IndexWriter (org.apache.lucene.index.IndexWriter)14 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)8 BinaryPoint (org.apache.lucene.document.BinaryPoint)8 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)8 StoredField (org.apache.lucene.document.StoredField)8 Term (org.apache.lucene.index.Term)7 BytesRef (org.apache.lucene.util.BytesRef)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)5