Search in sources :

Example 46 with TopDocs

use of org.apache.lucene.search.TopDocs in project elasticsearch by elastic.

the class SimpleAllTests method testSimpleAllNoBoost.

public void testSimpleAllNoBoost() throws Exception {
    FieldType allFt = getAllFieldType();
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document doc = new Document();
    doc.add(new Field("_id", "1", StoredField.TYPE));
    doc.add(new AllField("_all", "something", 1.0f, allFt));
    doc.add(new AllField("_all", "else", 1.0f, allFt));
    indexWriter.addDocument(doc);
    doc = new Document();
    doc.add(new Field("_id", "2", StoredField.TYPE));
    doc.add(new AllField("_all", "else", 1.0f, allFt));
    doc.add(new AllField("_all", "something", 1.0f, allFt));
    indexWriter.addDocument(doc);
    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new AllTermQuery(new Term("_all", "else"));
    TopDocs docs = searcher.search(query, 10);
    assertThat(docs.totalHits, equalTo(2));
    assertThat(docs.scoreDocs[0].doc, equalTo(0));
    assertExplanationScore(searcher, query, docs.scoreDocs[0]);
    assertThat(docs.scoreDocs[1].doc, equalTo(1));
    assertExplanationScore(searcher, query, docs.scoreDocs[1]);
    query = new AllTermQuery(new Term("_all", "something"));
    docs = searcher.search(query, 10);
    assertThat(docs.totalHits, equalTo(2));
    assertThat(docs.scoreDocs[0].doc, equalTo(0));
    assertExplanationScore(searcher, query, docs.scoreDocs[0]);
    assertThat(docs.scoreDocs[1].doc, equalTo(1));
    assertExplanationScore(searcher, query, docs.scoreDocs[1]);
    indexWriter.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) FieldType(org.apache.lucene.document.FieldType) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 47 with TopDocs

use of org.apache.lucene.search.TopDocs in project OpenGrok by OpenGrok.

the class IndexDatabase method getDefinitions.

/**
     * Get the latest definitions for a file from the index.
     *
     * @param file the file whose definitions to find
     * @return definitions for the file, or {@code null} if they could not be
     * found
     * @throws IOException if an error happens when accessing the index
     * @throws ParseException if an error happens when building the Lucene query
     * @throws ClassNotFoundException if the class for the stored definitions
     * instance cannot be found
     */
public static Definitions getDefinitions(File file) throws IOException, ParseException, ClassNotFoundException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String path = env.getPathRelativeToSourceRoot(file, 0);
    //sanitize windows path delimiters
    //in order not to conflict with Lucene escape character
    path = path.replace("\\", "/");
    IndexReader ireader = getIndexReader(path);
    if (ireader == null) {
        // No index, no definitions...
        return null;
    }
    try {
        Query q = new QueryBuilder().setPath(path).build();
        IndexSearcher searcher = new IndexSearcher(ireader);
        TopDocs top = searcher.search(q, 1);
        if (top.totalHits == 0) {
            // No hits, no definitions...
            return null;
        }
        Document doc = searcher.doc(top.scoreDocs[0].doc);
        String foundPath = doc.get(QueryBuilder.PATH);
        // Only use the definitions if we found an exact match.
        if (path.equals(foundPath)) {
            IndexableField tags = doc.getField(QueryBuilder.TAGS);
            if (tags != null) {
                return Definitions.deserialize(tags.binaryValue().bytes);
            }
        }
    } finally {
        ireader.close();
    }
    // Didn't find any definitions.
    return null;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Document(org.apache.lucene.document.Document)

Example 48 with TopDocs

use of org.apache.lucene.search.TopDocs in project ansj_seg by NLPchina.

the class HeightLightTest method search.

private static void search(Analyzer analyzer, Query query) throws IOException {
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    // 查询索引
    IndexSearcher isearcher = new IndexSearcher(directoryReader);
    System.out.println(query);
    TopDocs hits = isearcher.search(query, 5);
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        int docId = hits.scoreDocs[i].doc;
        Document document = isearcher.doc(docId);
        System.out.println(toHighlighter(analyzer, query, document));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document)

Example 49 with TopDocs

use of org.apache.lucene.search.TopDocs in project ansj_seg by NLPchina.

the class IndexTest method search.

private void search(Analyzer queryAnalyzer, Directory directory, String queryStr) throws CorruptIndexException, IOException, ParseException {
    IndexSearcher isearcher;
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    // 查询索引
    isearcher = new IndexSearcher(directoryReader);
    QueryParser tq = new QueryParser("text", queryAnalyzer);
    Query query = tq.parse(queryStr);
    System.out.println(query);
    TopDocs hits = isearcher.search(query, 5);
    System.out.println(queryStr + ":共找到" + hits.totalHits + "条记录!");
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        int docId = hits.scoreDocs[i].doc;
        Document document = isearcher.doc(docId);
        System.out.println(toHighlighter(queryAnalyzer, query, document));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document)

Example 50 with TopDocs

use of org.apache.lucene.search.TopDocs in project ansj_seg by NLPchina.

the class IndexTest method search.

private void search(Analyzer queryAnalyzer, Directory directory, String queryStr) throws CorruptIndexException, IOException, ParseException {
    IndexSearcher isearcher;
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    // 查询索引
    isearcher = new IndexSearcher(directoryReader);
    QueryParser tq = new QueryParser("text", queryAnalyzer);
    Query query = tq.parse(queryStr);
    System.out.println(query);
    TopDocs hits = isearcher.search(query, 5);
    System.out.println(queryStr + ":共找到" + hits.totalHits + "条记录!");
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        int docId = hits.scoreDocs[i].doc;
        Document document = isearcher.doc(docId);
        System.out.println(toHighlighter(queryAnalyzer, query, document));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document)

Aggregations

TopDocs (org.apache.lucene.search.TopDocs)496 IndexSearcher (org.apache.lucene.search.IndexSearcher)302 Document (org.apache.lucene.document.Document)275 TermQuery (org.apache.lucene.search.TermQuery)189 IndexReader (org.apache.lucene.index.IndexReader)187 Term (org.apache.lucene.index.Term)174 Directory (org.apache.lucene.store.Directory)174 Query (org.apache.lucene.search.Query)172 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)144 BooleanQuery (org.apache.lucene.search.BooleanQuery)127 ScoreDoc (org.apache.lucene.search.ScoreDoc)127 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)120 Sort (org.apache.lucene.search.Sort)94 Field (org.apache.lucene.document.Field)85 SortField (org.apache.lucene.search.SortField)74 IOException (java.io.IOException)58 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)56 TextField (org.apache.lucene.document.TextField)47 PhraseQuery (org.apache.lucene.search.PhraseQuery)46 PrefixQuery (org.apache.lucene.search.PrefixQuery)45