gpt4 book ai didi

java - 使用 Lucene 进行精确短语搜索?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:24 28 4
gpt4 key购买 nike

我正在使用 SpanTerm 查询在 lucene 中搜索精确的短语。但这似乎不起作用。这是我的代码。

索引

IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), false,IndexWriter.MaxFieldLength.UNLIMITED);  
doc.add(new Field("contents", sb.toString(), Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.add(new Field("imageid", imageDocument.getImageId(), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("title", imageDocument.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("country", imageDocument.getCountry(), Field.Store.YES, Field.Index.NOT_ANALYZED));
write.addDocument(doc);

搜索

String sentence = searchParameters.get("searchExactWord");
String[] words = sentence.split(" ");
String queryNoWord = "";
int i = 0;
SpanTermQuery [] clause = new SpanTermQuery[words.length];
for (String word : words)
{
clause[i] = new SpanTermQuery(new Term("contents",word));
i++;
}
SpanNearQuery query = new SpanNearQuery(clause, 0, true);
booleanQuery.add(query, BooleanClause.Occur.MUST);

如果我做错了,请指导我???

帕特克

最佳答案

试试PhraseQuery相反:

PhraseQuery query = new PhraseQuery();
String[] words = sentence.split(" ");
for (String word : words) {
query.add(new Term("contents", word));
}
booleanQuery.add(query, BooleanClause.Occur.MUST);

编辑:我认为您遇到了不同的问题。 booleanQuery 还有哪些其他部分?以下是搜索短语的完整示例:

public class LucenePhraseQuery {
public static void main(String[] args) throws Exception {
// setup Lucene to use an in-memory index
Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
MaxFieldLength mlf = MaxFieldLength.UNLIMITED;
IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);

// index a few documents
writer.addDocument(createDocument("1", "foo bar baz"));
writer.addDocument(createDocument("2", "red green blue"));
writer.addDocument(createDocument("3", "test foo bar test"));
writer.close();

// search for documents that have "foo bar" in them
String sentence = "foo bar";
IndexSearcher searcher = new IndexSearcher(directory);
PhraseQuery query = new PhraseQuery();
String[] words = sentence.split(" ");
for (String word : words) {
query.add(new Term("contents", word));
}

// display search results
TopDocs topDocs = searcher.search(query, 10);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc);
}
}

private static Document createDocument(String id, String content) {
Document doc = new Document();
doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("contents", content, Store.YES, Index.ANALYZED,
Field.TermVector.WITH_POSITIONS_OFFSETS));
return doc;
}
}

关于java - 使用 Lucene 进行精确短语搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5527868/

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