gpt4 book ai didi

java - 根据具有相同名称的特定字段的权重调整 Lucene 搜索结果分数

转载 作者:搜寻专家 更新时间:2023-11-01 02:53:55 26 4
gpt4 key购买 nike

我目前使用 Lucene 作为我们的全文搜索引擎。但是我们需要根据特定字段对搜索结果进行排序。

例如,如果我们的索引中有以下三个文档,除了 id 字段之外的内容完全相同。

    val document01 = new Document()
val field0100 = new Field("id", "1", Field.Store.YES, Field.Index.ANALYZED)
val field0101 = new Field("contents", "This is a test: Linux", Field.Store.YES, Field.Index.ANALYZED)
val field0102 = new Field("contents", "This is a test: Windows", Field.Store.YES, Field.Index.ANALYZED)
document01.add(field0100)
document01.add(field0101)
document01.add(field0102)

val document02 = new Document()
val field0200 = new Field("id", "2", Field.Store.YES, Field.Index.ANALYZED)
val field0201 = new Field("contents", "This is a test: Linux", Field.Store.YES, Field.Index.ANALYZED)
val field0202 = new Field("contents", "This is a test: Windows", Field.Store.YES, Field.Index.ANALYZED)
document02.add(field0200)
document02.add(field0201)
document02.add(field0202)

val document03 = new Document()
val field0300 = new Field("id", "3", Field.Store.YES, Field.Index.ANALYZED)
val field0301 = new Field("contents", "This is a test: Linux", Field.Store.YES, Field.Index.ANALYZED)
val field0302 = new Field("contents", "This is a test: Windows", Field.Store.YES, Field.Index.ANALYZED)
document03.add(field0300)
document03.add(field0301)
document03.add(field0302)

现在,当我使用 IndexSearcher 搜索 Linux 时,我得到以下结果:

Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>

当我搜索 Windows 时,我得到相同顺序的相同结果。

Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>

问题是建索引时是否可以对特定字段进行加权?例如,如果在搜索时匹配到 field0201,我想让它获得更高的分数。

换句话说,当我搜索 Linux 时,我希望按以下顺序获得结果:

Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>

当我搜索 Windows 时,它仍然保持原来的顺序,如下所示:

Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>
Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>>

我尝试使用 field0201.setBoost(),但是当我搜索 LinuxWindows 时,它会改变搜索结果的顺序。

最佳答案

我认为如果将不同来源的数据放在具有不同名称的字段中应该是可能的。您可以在索引时设置提升,但如果您使用相同的名称,我认为提升将应用于具有相同名称的所有字段 - 基于 setBoost javadoc .因此,如果您改为这样做:

val field0201 = new Field("content-high", "This is a test: Linux", ...)
field0201.setBoost(1.5f)
val field0202 = new Field("content-low", "This is a test: Windows", ...)

然后使用 content-high:Linux content-low:Linux 进行查询(使用带有两个 should 子句的 boolean 查询都设置为术语 Linux),那么如果匹配项在该字段中,则 content-high 的提升应该会增加文档分数。使用 explain看看是否有效。

关于java - 根据具有相同名称的特定字段的权重调整 Lucene 搜索结果分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5657848/

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