Lucene索引具有相同性质的多个字段

blu*_*iel 7 lucene indexing zend-framework

每个Lucene doc都是一个配方,每个配方都有成分.

我努力寻找成分,并给出一个结果,说两个成分匹配四个.(例如)

那么如何将这些成分添加到doc中呢?在solr我可以创建多个字段,它会将它们全部保存,我可能做错了,因为它只保存了一个成分.

这也适用于像'tags'这样的字段.

ps我正在使用Zend Framework,如果它很重要的话.

Joe*_*oel 14

Lucene文档支持添加多个同名字段.即你可以反复打电话:

document.add(new Field("name"), value) 
Run Code Online (Sandbox Code Playgroud)

所以你这样做:

# (pseudo-code) 
document1.add(new Field("ingredient"), "vanilla") 
document1.add(new Field("ingredient"), "strawberry") 
index.add(document)

# And then search for
index.search("ingredient", "vanilla" && "strawberry")
Run Code Online (Sandbox Code Playgroud)

你会得到回文件1.但是如果你搜索:

index.search("ingredient", "vanilla" && "apple")
Run Code Online (Sandbox Code Playgroud)

你不会得到document1.

如果您搜索:

index.search("ingredient", "vanilla" || "apple")
Run Code Online (Sandbox Code Playgroud)

你也可以找回document1.

如果要查看哪些成分匹配,只需将文档上的字段保存为存储字段,然后为每个匹配的文档检索字段列表并将其与用户查询进行比较.

另请注意,默认情况下,添加到文档的具有相同名称的字段的PositionIncrementGap为0.

这意味着,如果你添加:

   document1.add(new Field("ingredient"), "chocolate") 
   document1.add(new Field("ingredient"), "orange") 
Run Code Online (Sandbox Code Playgroud)

然后它会被视为一种叫做"巧克力橙"的单一成分,可能与之匹配:

index.search("ingredient", "chocolate orange")
Run Code Online (Sandbox Code Playgroud)

您可以避免为PositionIncrementGap> 1设置一个值,这将产生:

0场比赛:

index.search("ingredient", "chocolate orange")
Run Code Online (Sandbox Code Playgroud)

和1匹配:

index.search("ingredient", "chocolate" &&  "orange")
Run Code Online (Sandbox Code Playgroud)