All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metaeffekt.artifact.resolver.maven.index.lucene.MavenLuceneIndex Maven / Gradle / Ivy

package org.metaeffekt.artifact.resolver.maven.index.lucene;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.metaeffekt.artifact.resolver.generic.index.lucene.AbstractLuceneIndex;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Maven lucene index support resolver-level queries.
 */
@Slf4j
public class MavenLuceneIndex extends AbstractLuceneIndex {

    private final IndexReader reader;
    private final IndexSearcher searcher;

    public MavenLuceneIndex(@NonNull File indexDir, @NonNull Analyzer analyzer) throws IOException {
        super(indexDir, analyzer);

        this.reader = DirectoryReader.open(getDirectory());
        this.searcher = new IndexSearcher(reader);
    }

    /**
     * Lookup value (being whatever substring shall be included) in key.
     *
     * @param key field name to search
     * @param value value to search for
     * @param n maximum number of documents to output
     *
     * @return found documents
     *
     * @throws IOException on failure to open / search index
     */
    @NonNull
    public List lookupContains(@NonNull String key, @NonNull String value, int n) throws IOException {
        return runQuery(new TermQuery(new Term(key, value)), n, searcher);
    }

    @NonNull
    public List runQuery(Query query, int n) throws IOException {
        return runQuery(query, n, searcher);
    }

    public long size() {
        return reader.numDocs();
    }

    /**
     * Closes underlying index interfaces.
     */
    @Override
    public void close() throws Exception {
        reader.close();
        super.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy