Search in sources :

Example 51 with IndexReader

use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.

the class ParentFieldSubFetchPhaseTests method testGetParentId.

public void testGetParentId() throws Exception {
    ParentFieldMapper fieldMapper = createParentFieldMapper();
    Directory directory = newDirectory();
    IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig());
    Document document = new Document();
    document.add(new SortedDocValuesField(fieldMapper.fieldType().name(), new BytesRef("1")));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0);
    assertEquals("1", id);
    indexReader.close();
    directory.close();
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) IndexWriter(org.apache.lucene.index.IndexWriter) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 52 with IndexReader

use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.

the class QueryPhaseTests method countTestCase.

private void countTestCase(boolean withDeletions) throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    final int numDocs = scaledRandomIntBetween(100, 200);
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        if (randomBoolean()) {
            doc.add(new StringField("foo", "bar", Store.NO));
        }
        if (randomBoolean()) {
            doc.add(new StringField("foo", "baz", Store.NO));
        }
        if (withDeletions && (rarely() || i == 0)) {
            doc.add(new StringField("delete", "yes", Store.NO));
        }
        w.addDocument(doc);
    }
    if (withDeletions) {
        w.deleteDocuments(new Term("delete", "yes"));
    }
    final IndexReader reader = w.getReader();
    Query matchAll = new MatchAllDocsQuery();
    Query matchAllCsq = new ConstantScoreQuery(matchAll);
    Query tq = new TermQuery(new Term("foo", "bar"));
    Query tCsq = new ConstantScoreQuery(tq);
    BooleanQuery bq = new BooleanQuery.Builder().add(matchAll, Occur.SHOULD).add(tq, Occur.MUST).build();
    countTestCase(matchAll, reader, false);
    countTestCase(matchAllCsq, reader, false);
    countTestCase(tq, reader, withDeletions);
    countTestCase(tCsq, reader, withDeletions);
    countTestCase(bq, reader, true);
    reader.close();
    w.close();
    dir.close();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 53 with IndexReader

use of org.apache.lucene.index.IndexReader in project neo4j by neo4j.

the class FullTxData method searcher.

private IndexSearcher searcher(boolean allowRefreshSearcher) {
    if (this.searcher != null && (!modified || !allowRefreshSearcher)) {
        return this.searcher;
    }
    try {
        IndexReader newReader = this.reader == null ? DirectoryReader.open(this.writer) : DirectoryReader.openIfChanged((DirectoryReader) this.reader);
        if (newReader == null) {
            return this.searcher;
        }
        LuceneUtil.close(reader);
        this.reader = newReader;
        LuceneUtil.close(searcher);
        searcher = new IndexSearcher(reader);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (allowRefreshSearcher) {
            this.modified = false;
        }
    }
    return this.searcher;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException)

Example 54 with IndexReader

use of org.apache.lucene.index.IndexReader in project textdb by TextDB.

the class WordCountIndexSource method computeWordCount.

private void computeWordCount() throws TextDBException {
    try {
        HashMap<String, Integer> wordCountMap = new HashMap<>();
        DataReader dataReader = RelationManager.getRelationManager().getTableDataReader(predicate.getTableName(), new MatchAllDocsQuery());
        dataReader.open();
        IndexReader luceneIndexReader = dataReader.getLuceneIndexReader();
        for (int i = 0; i < luceneIndexReader.numDocs(); i++) {
            Terms termVector = luceneIndexReader.getTermVector(i, predicate.getAttribute());
            TermsEnum termsEnum = termVector.iterator();
            while (termsEnum.next() != null) {
                String key = termsEnum.term().utf8ToString();
                wordCountMap.put(key, wordCountMap.get(key) == null ? ((int) termsEnum.totalTermFreq()) : wordCountMap.get(key) + ((int) termsEnum.totalTermFreq()));
            }
        }
        luceneIndexReader.close();
        dataReader.close();
        sortedWordCountMap = wordCountMap.entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())).collect(Collectors.toList());
        wordCountIterator = sortedWordCountMap.iterator();
    } catch (IOException e) {
        throw new DataFlowException(e);
    }
}
Also used : DataReader(edu.uci.ics.textdb.storage.DataReader) HashMap(java.util.HashMap) IndexReader(org.apache.lucene.index.IndexReader) Terms(org.apache.lucene.index.Terms) DataFlowException(edu.uci.ics.textdb.api.exception.DataFlowException) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 55 with IndexReader

use of org.apache.lucene.index.IndexReader in project intellij-community by JetBrains.

the class Maven2ServerIndexerImpl method processArtifacts.

public void processArtifacts(int indexId, MavenServerIndicesProcessor processor) throws MavenServerIndexerException {
    try {
        final int CHUNK_SIZE = 10000;
        IndexReader r = getIndex(indexId).getIndexReader();
        int total = r.numDocs();
        List<MavenId> result = new ArrayList<MavenId>(Math.min(CHUNK_SIZE, total));
        for (int i = 0; i < total; i++) {
            if (r.isDeleted(i))
                continue;
            Document doc = r.document(i);
            String uinfo = doc.get(SEARCH_TERM_COORDINATES);
            if (uinfo == null)
                continue;
            List<String> parts = StringUtil.split(uinfo, "|");
            String groupId = parts.get(0);
            String artifactId = parts.get(1);
            String version = parts.get(2);
            if (groupId == null || artifactId == null || version == null)
                continue;
            result.add(new MavenId(groupId, artifactId, version));
            if (result.size() == CHUNK_SIZE) {
                processor.processArtifacts(result);
                result.clear();
            }
        }
        if (!result.isEmpty()) {
            processor.processArtifacts(result);
        }
    } catch (Exception e) {
        throw new MavenServerIndexerException(wrapException(e));
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ArchetypeDataSourceException(org.apache.maven.archetype.source.ArchetypeDataSourceException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)962 Document (org.apache.lucene.document.Document)610 Directory (org.apache.lucene.store.Directory)603 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)549 IndexSearcher (org.apache.lucene.search.IndexSearcher)410 Term (org.apache.lucene.index.Term)332 TopDocs (org.apache.lucene.search.TopDocs)204 TermQuery (org.apache.lucene.search.TermQuery)160 Query (org.apache.lucene.search.Query)158 IndexWriter (org.apache.lucene.index.IndexWriter)150 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)144 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)143 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)142 Field (org.apache.lucene.document.Field)135 BytesRef (org.apache.lucene.util.BytesRef)134 IOException (java.io.IOException)133 BooleanQuery (org.apache.lucene.search.BooleanQuery)122 ArrayList (java.util.ArrayList)108 TextField (org.apache.lucene.document.TextField)81 Test (org.junit.Test)81