Search in sources :

Example 1 with LegacyIntField

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

the class SimpleLuceneTests method testNumericTermDocsFreqs.

/**
     * A test just to verify that term freqs are not stored for numeric fields. <tt>int1</tt> is not storing termFreq
     * and <tt>int2</tt> does.
     */
public void testNumericTermDocsFreqs() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document doc = new Document();
    FieldType type = LegacyIntField.TYPE_NOT_STORED;
    LegacyIntField field = new LegacyIntField("int1", 1, type);
    doc.add(field);
    type = new FieldType(LegacyIntField.TYPE_NOT_STORED);
    type.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
    type.freeze();
    field = new LegacyIntField("int1", 1, type);
    doc.add(field);
    field = new LegacyIntField("int2", 1, type);
    doc.add(field);
    field = new LegacyIntField("int2", 1, type);
    doc.add(field);
    indexWriter.addDocument(doc);
    IndexReader reader = DirectoryReader.open(indexWriter);
    LeafReader atomicReader = SlowCompositeReaderWrapper.wrap(reader);
    Terms terms = atomicReader.terms("int1");
    TermsEnum termsEnum = terms.iterator();
    termsEnum.next();
    PostingsEnum termDocs = termsEnum.postings(null);
    assertThat(termDocs.nextDoc(), equalTo(0));
    assertThat(termDocs.docID(), equalTo(0));
    assertThat(termDocs.freq(), equalTo(1));
    terms = atomicReader.terms("int2");
    termsEnum = terms.iterator();
    termsEnum.next();
    termDocs = termsEnum.postings(termDocs);
    assertThat(termDocs.nextDoc(), equalTo(0));
    assertThat(termDocs.docID(), equalTo(0));
    assertThat(termDocs.freq(), equalTo(2));
    reader.close();
    indexWriter.close();
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) Terms(org.apache.lucene.index.Terms) Document(org.apache.lucene.document.Document) PostingsEnum(org.apache.lucene.index.PostingsEnum) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) FieldType(org.apache.lucene.document.FieldType) LegacyIntField(org.apache.lucene.document.LegacyIntField) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 2 with LegacyIntField

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

the class SimpleLuceneTests method testSimpleNumericOps.

public void testSimpleNumericOps() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document document = new Document();
    document.add(new TextField("_id", "1", Field.Store.YES));
    document.add(new LegacyIntField("test", 2, LegacyIntField.TYPE_STORED));
    indexWriter.addDocument(document);
    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);
    Document doc = searcher.doc(topDocs.scoreDocs[0].doc);
    IndexableField f = doc.getField("test");
    assertThat(f.stringValue(), equalTo("2"));
    BytesRefBuilder bytes = new BytesRefBuilder();
    LegacyNumericUtils.intToPrefixCoded(2, 0, bytes);
    topDocs = searcher.search(new TermQuery(new Term("test", bytes.get())), 1);
    doc = searcher.doc(topDocs.scoreDocs[0].doc);
    f = doc.getField("test");
    assertThat(f.stringValue(), equalTo("2"));
    indexWriter.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) LegacyIntField(org.apache.lucene.document.LegacyIntField)

Example 3 with LegacyIntField

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

the class NestedChildrenFilterTests method testNestedChildrenFilter.

public void testNestedChildrenFilter() throws Exception {
    int numParentDocs = scaledRandomIntBetween(0, 32);
    int maxChildDocsPerParent = scaledRandomIntBetween(8, 16);
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    for (int i = 0; i < numParentDocs; i++) {
        int numChildDocs = scaledRandomIntBetween(0, maxChildDocsPerParent);
        List<Document> docs = new ArrayList<>(numChildDocs + 1);
        for (int j = 0; j < numChildDocs; j++) {
            Document childDoc = new Document();
            childDoc.add(new StringField("type", "child", Field.Store.NO));
            docs.add(childDoc);
        }
        Document parenDoc = new Document();
        parenDoc.add(new StringField("type", "parent", Field.Store.NO));
        parenDoc.add(new LegacyIntField("num_child_docs", numChildDocs, Field.Store.YES));
        docs.add(parenDoc);
        writer.addDocuments(docs);
    }
    IndexReader reader = writer.getReader();
    writer.close();
    IndexSearcher searcher = new IndexSearcher(reader);
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
    Query childFilter = new TermQuery(new Term("type", "child"));
    int checkedParents = 0;
    final Weight parentsWeight = searcher.createNormalizedWeight(new TermQuery(new Term("type", "parent")), false);
    for (LeafReaderContext leaf : reader.leaves()) {
        DocIdSetIterator parents = parentsWeight.scorer(leaf).iterator();
        for (int parentDoc = parents.nextDoc(); parentDoc != DocIdSetIterator.NO_MORE_DOCS; parentDoc = parents.nextDoc()) {
            int expectedChildDocs = leaf.reader().document(parentDoc).getField("num_child_docs").numericValue().intValue();
            hitContext.reset(null, leaf, parentDoc, searcher);
            NestedChildrenQuery nestedChildrenFilter = new NestedChildrenQuery(parentFilter, childFilter, hitContext);
            TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
            searcher.search(new ConstantScoreQuery(nestedChildrenFilter), totalHitCountCollector);
            assertThat(totalHitCountCollector.getTotalHits(), equalTo(expectedChildDocs));
            checkedParents++;
        }
    }
    assertThat(checkedParents, equalTo(numParentDocs));
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) NestedChildrenQuery(org.elasticsearch.search.fetch.subphase.InnerHitsContext.NestedInnerHits.NestedChildrenQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) Weight(org.apache.lucene.search.Weight) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) NestedChildrenQuery(org.elasticsearch.search.fetch.subphase.InnerHitsContext.NestedInnerHits.NestedChildrenQuery) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) LegacyIntField(org.apache.lucene.document.LegacyIntField)

Aggregations

Document (org.apache.lucene.document.Document)3 LegacyIntField (org.apache.lucene.document.LegacyIntField)3 IndexReader (org.apache.lucene.index.IndexReader)3 Directory (org.apache.lucene.store.Directory)3 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 Term (org.apache.lucene.index.Term)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 TermQuery (org.apache.lucene.search.TermQuery)2 RAMDirectory (org.apache.lucene.store.RAMDirectory)2 ArrayList (java.util.ArrayList)1 FieldType (org.apache.lucene.document.FieldType)1 StringField (org.apache.lucene.document.StringField)1 TextField (org.apache.lucene.document.TextField)1 IndexableField (org.apache.lucene.index.IndexableField)1 LeafReader (org.apache.lucene.index.LeafReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 PostingsEnum (org.apache.lucene.index.PostingsEnum)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 Terms (org.apache.lucene.index.Terms)1