gpt4 book ai didi

java - 你如何读取 Lucene 中的索引来进行搜索?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:47:55 25 4
gpt4 key购买 nike

Lucene 4.3 新手

如何在 Lucene 4.3 中进行简单的搜索?

我在一个简单的 Java 测试用例中修改了大纲: http://lucene.apache.org/core/4_3_0/core/overview-summary.html#overview_description

示例开始于:

DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);

但是根据文档,DirectoryReader 是不可见的( protected )。因此,您似乎无法使用 DirectoryReader。

所以我进行了挖掘并尝试了各种排列以避免直接使用 DirectoryReader,包括:

File indexdir = new File("D:\\lucenetest\\") ; // location of my index
Directory directory = FSDirectory.open(indexdir);

IndexReader ireader = IndexReader.open(FSDirectory.open(indexdir)); //ERROR NoSuchMethodError
//IndexReader ireader = IndexReader.open(directory); //variation ERROR NoSuchMethodError
IndexSearcher isearcher = new IndexSearcher(ireader);

等等。 (包括试用 AtomicReaders)。似乎没有任何效果。 (我确认 Lucene Core 已正确导入。)索引工作正常。

我查看了 Lucene 示例搜索代码以获取更多线索。 http://lucene.apache.org/core/4_2_1/demo/src-html/org/apache/lucene/demo/SearchFiles.html

IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); //DirectoryReader not visible error
IndexSearcher searcher = new IndexSearcher(reader);

这在简单示例文件中使用时也不起作用。

我已经能够让简单的索引工作,并且之前能够让 Lucene 演示工作(索引和搜索)。但是,我似乎无法进行简单的搜索。

有什么线索吗?

最佳答案

可以使用此示例代码执行非常简单的搜索

// directory where your index is stored
File path = new File(" ... /solr/solr/Collection1/data/index");

Directory index = FSDirectory.open(path);
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);

Term t = new Term("myfield", "myvalue");

// Get the top 10 docs
Query query = new TermQuery(t);
TopDocs tops= searcher.search(query, 10);
ScoreDoc[] scoreDoc = tops.scoreDocs;
System.out.println(scoreDoc.length);
for (ScoreDoc score : scoreDoc){
System.out.println("DOC " + score.doc + " SCORE " + score.score);
}

// Get the frequency of the term
int freq = reader.docFreq(t);
System.out.println("FREQ " + freq);
`

关于java - 你如何读取 Lucene 中的索引来进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16847857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com