gpt4 book ai didi

java - 在 lucene 中搜索 UUID 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:32:00 26 4
gpt4 key购买 nike

我有一个要添加到文档中的 UUID 字段,格式如下:372d325c-e01b-432f-98bd-bc4c949f15b8。但是,当我尝试通过 UUID 查询文档时,无论我如何尝试转义表达式,它都不会返回它们。例如:

+uuid:372d325c-e01b-432f-98bd-bc4c949f15b8
+uuid:"372d325c-e01b-432f-98bd-bc4c949f15b8"
+uuid:372d325c\-e01b\-432f\-98bd\-bc4c949f15b8
+uuid:(372d325c-e01b-432f-98bd-bc4c949f15b8)
+uuid:("372d325c-e01b-432f-98bd-bc4c949f15b8")

甚至像这样使用 TermQuery 完全跳过 QueryParser:

new TermQuery(new Term("uuid", uuid.toString()))

或者

new TermQuery(new Term("uuid", QueryParser.escape(uuid.toString())))

这些搜索都不会返回文档,但如果我搜索 UUID 的一部分,它将返回一个文档。例如,这些将返回一些东西:

+uuid:372d325c
+uuid:e01b
+uuid:432f

我应该怎么做才能索引这些文档,以便我可以通过它们的 UUID 将它们拉回来?我考虑过重新格式化 UUID 以删除连字符,但我还没有实现它。

最佳答案

我让它工作的唯一方法是使用 WhitespaceAnalyzer 而不是 StandardAnalyzer。然后像这样使用 TermQuery:

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, new WhitespaceAnalyzer(Version.LUCENE_36))
.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
writer = new IndexWriter( directory, config);

然后搜索:

TopDocs docs = searcher.search(new TermQuery(new Term("uuid", uuid.toString())), 1);

WhitespaceAnalyzer 阻止 Lucene 通过连字符拆分 UUID。另一种选择是从 UUID 中消除破折号,但使用 WhitespaceAnalyzer 也能达到我的目的。

关于java - 在 lucene 中搜索 UUID 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753642/

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