Search in sources :

Example 6 with Document

use of org.apache.lucene.document.Document in project crate by crate.

the class BooleanColumnReferenceTest method insertValues.

@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new StringField("_id", Integer.toString(i), Field.Store.NO));
        doc.add(new NumericDocValuesField(column, i % 2 == 0 ? 1 : 0));
        writer.addDocument(doc);
    }
}
Also used : NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document)

Example 7 with Document

use of org.apache.lucene.document.Document in project crate by crate.

the class DoubleColumnReferenceTest method insertValues.

@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (double d = 0.5; d < 10.0d; d++) {
        Document doc = new Document();
        doc.add(new StringField("_id", Double.toString(d), Field.Store.NO));
        doc.add(new DoubleField(column, d, Field.Store.NO));
        writer.addDocument(doc);
    }
}
Also used : StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) DoubleField(org.apache.lucene.document.DoubleField)

Example 8 with Document

use of org.apache.lucene.document.Document in project crate by crate.

the class LuceneOrderedDocCollectorTest method addDocToLucene.

private void addDocToLucene(IndexWriter w, Long value) throws IOException {
    Document doc = new Document();
    if (value != null) {
        doc.add(new LongFieldMapper.CustomLongNumericField(value, valueFieldType));
        doc.add(new SortedNumericDocValuesField("value", value));
    } else {
        // Create a placeholder field
        doc.add(new SortedDocValuesField("null_value", new BytesRef("null")));
    }
    w.addDocument(doc);
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) LongFieldMapper(org.elasticsearch.index.mapper.core.LongFieldMapper) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef)

Example 9 with Document

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

the class FiltersAggregatorTests method testKeyedFilter.

public void testKeyedFilter() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new Field("field", "foo", fieldType));
    indexWriter.addDocument(document);
    document.clear();
    document.add(new Field("field", "else", fieldType));
    indexWriter.addDocument(document);
    // make sure we have more than one segment to test the merge
    indexWriter.commit();
    document.add(new Field("field", "foo", fieldType));
    indexWriter.addDocument(document);
    document.clear();
    document.add(new Field("field", "bar", fieldType));
    indexWriter.addDocument(document);
    document.clear();
    document.add(new Field("field", "foobar", fieldType));
    indexWriter.addDocument(document);
    indexWriter.commit();
    document.clear();
    document.add(new Field("field", "something", fieldType));
    indexWriter.addDocument(document);
    indexWriter.commit();
    document.clear();
    document.add(new Field("field", "foobar", fieldType));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    FiltersAggregator.KeyedFilter[] keys = new FiltersAggregator.KeyedFilter[6];
    keys[0] = new FiltersAggregator.KeyedFilter("foobar", QueryBuilders.termQuery("field", "foobar"));
    keys[1] = new FiltersAggregator.KeyedFilter("bar", QueryBuilders.termQuery("field", "bar"));
    keys[2] = new FiltersAggregator.KeyedFilter("foo", QueryBuilders.termQuery("field", "foo"));
    keys[3] = new FiltersAggregator.KeyedFilter("foo2", QueryBuilders.termQuery("field", "foo"));
    keys[4] = new FiltersAggregator.KeyedFilter("same", QueryBuilders.termQuery("field", "foo"));
    // filter name already present so it should be merge with the previous one ?
    keys[5] = new FiltersAggregator.KeyedFilter("same", QueryBuilders.termQuery("field", "bar"));
    FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", keys);
    builder.otherBucket(true);
    builder.otherBucketKey("other");
    for (boolean doReduce : new boolean[] { true, false }) {
        final InternalFilters filters;
        if (doReduce) {
            filters = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
        } else {
            filters = search(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
        }
        assertEquals(filters.getBuckets().size(), 7);
        assertEquals(filters.getBucketByKey("foobar").getDocCount(), 2);
        assertEquals(filters.getBucketByKey("foo").getDocCount(), 2);
        assertEquals(filters.getBucketByKey("foo2").getDocCount(), 2);
        assertEquals(filters.getBucketByKey("bar").getDocCount(), 1);
        assertEquals(filters.getBucketByKey("same").getDocCount(), 1);
        assertEquals(filters.getBucketByKey("other").getDocCount(), 2);
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FiltersAggregator(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Field(org.apache.lucene.document.Field) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) IndexReader(org.apache.lucene.index.IndexReader) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 10 with Document

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

the class FiltersAggregatorTests method testRandom.

public void testRandom() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    int numDocs = randomIntBetween(100, 200);
    int maxTerm = randomIntBetween(10, 50);
    int[] expectedBucketCount = new int[maxTerm];
    Document document = new Document();
    for (int i = 0; i < numDocs; i++) {
        if (frequently()) {
            // make sure we have more than one segment to test the merge
            indexWriter.commit();
        }
        int value = randomInt(maxTerm - 1);
        expectedBucketCount[value] += 1;
        document.add(new Field("field", Integer.toString(value), fieldType));
        indexWriter.addDocument(document);
        document.clear();
    }
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    int numFilters = randomIntBetween(1, 10);
    QueryBuilder[] filters = new QueryBuilder[numFilters];
    int[] filterTerms = new int[numFilters];
    int expectedOtherCount = numDocs;
    Set<Integer> filterSet = new HashSet<>();
    for (int i = 0; i < filters.length; i++) {
        int value = randomInt(maxTerm - 1);
        filters[i] = QueryBuilders.termQuery("field", Integer.toString(value));
        filterTerms[i] = value;
        if (filterSet.contains(value) == false) {
            expectedOtherCount -= expectedBucketCount[value];
            filterSet.add(value);
        }
    }
    FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters);
    builder.otherBucket(true);
    builder.otherBucketKey("other");
    for (boolean doReduce : new boolean[] { true, false }) {
        final InternalFilters response;
        if (doReduce) {
            response = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
        } else {
            response = search(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
        }
        List<InternalFilters.InternalBucket> buckets = response.getBuckets();
        assertEquals(buckets.size(), filters.length + 1);
        for (InternalFilters.InternalBucket bucket : buckets) {
            if ("other".equals(bucket.getKey())) {
                assertEquals(bucket.getDocCount(), expectedOtherCount);
            } else {
                int index = Integer.parseInt(bucket.getKey());
                assertEquals(bucket.getDocCount(), (long) expectedBucketCount[filterTerms[index]]);
            }
        }
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Field(org.apache.lucene.document.Field) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) IndexReader(org.apache.lucene.index.IndexReader) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Aggregations

Document (org.apache.lucene.document.Document)2344 Directory (org.apache.lucene.store.Directory)1374 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)798 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)752 IndexReader (org.apache.lucene.index.IndexReader)598 Field (org.apache.lucene.document.Field)480 IndexSearcher (org.apache.lucene.search.IndexSearcher)470 Term (org.apache.lucene.index.Term)456 BytesRef (org.apache.lucene.util.BytesRef)415 StringField (org.apache.lucene.document.StringField)403 TextField (org.apache.lucene.document.TextField)389 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)325 IndexWriter (org.apache.lucene.index.IndexWriter)312 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)278 TopDocs (org.apache.lucene.search.TopDocs)270 TermQuery (org.apache.lucene.search.TermQuery)237 FieldType (org.apache.lucene.document.FieldType)231 DirectoryReader (org.apache.lucene.index.DirectoryReader)226 Test (org.junit.Test)222 RAMDirectory (org.apache.lucene.store.RAMDirectory)211