简体   繁体   English

Elasticsearch 突出显示不在查询中的字段

[英]Elasticsearch highlight fields not in query

I have a document with many fields in it, each field containing short texts.我有一个包含许多字段的文档,每个字段都包含短文本。

To enable custom boosting and to keep performance problems at bay I created groupings of these fields according to their boost level.为了启用自定义提升并避免出现性能问题,我根据提升级别创建了这些字段的分组。 Example:例子:

"partName": {
    "type": "text",
    "copy_to": "fuzzyMatchFields_boost5"
},

This means that to match partName I am going to do a fuzzy matching search on fuzzyMatchFields_boost5 and boost the score 5 times.这意味着为了匹配partName ,我将对fuzzyMatchFields_boost5进行模糊匹配搜索并将分数提高5 倍。 The query is something like:查询类似于:

QueryBuilders.boolQuery()
    .must(
        QueryBuilders.multiMatchQuery(terms)
            .field("exactMatchFields_boost5", 5f)
            .field("exactMatchFields_boost2", 2f)
            .field("fuzzyMatchFields_boost5", 5f)
            .field("fuzzyMatchFields_boost4", 4f)
            .field("fuzzyMatchFields_boost3", 3f)
            .field("fuzzyMatchFields_boost2", 2f)
    )
...

etc. (I am using the Java client, but that's just btw.)等等(我正在使用 Java 客户端,但这只是顺便说一句。)

This works fine, but now I'm trying to add highlighting of matches.这很好用,但现在我正在尝试添加匹配的突出显示。

The problem is that I do not query the fields directly, rather than their grouping fields only.问题是我不直接查询字段,而不是只查询它们的分组字段。 So when I try highlighting I get nothing.因此,当我尝试突出显示时,我什么也得不到。

I already figured out that I can toggle a flag ( require_field_match ) and just pass the field names regardless:我已经发现我可以切换一个标志( require_field_match )并传递字段名称,不管:

val highlightBuilder = HighlightBuilder()
    .requireFieldMatch(false)
highlightFields.forEach { field ->
    highlightBuilder.field(field)
}

This works in some cases, but it doesn't work for others.这在某些情况下有效,但对其他人无效。 I cannot figure out why certain fields work and why certain fields don't.我无法弄清楚为什么某些字段有效,为什么某些字段不有效。 I'm not even sure if this is supposed to work.我什至不确定这是否应该工作。

A possible solution is to define another query specifically for the highlighting (this is possible in Elasticsearch).一种可能的解决方案是专门为突出显示定义另一个查询(这在 Elasticsearch 中是可能的)。 My concerns are around performance, ie.我担心的是性能,即。 this would obviously require me listing all the fields individually.这显然需要我单独列出所有字段。 I don't even understand why I would need a query at all, as I thought that for any matching document the highlighter just processes them individually.我什至不明白为什么我需要一个查询,因为我认为对于任何匹配的文档,荧光笔只是单独处理它们。 But maybe this is not the case.但也许情况并非如此。

What is the best solution here?这里最好的解决方案是什么?

This did the trick:这成功了:

Elasticsearch highlighting: "multi-match" query on custom "_all"-fields created with "copy_to" Elasticsearch 突出显示:对使用“copy_to”创建的自定义“_all”字段的“多匹配”查询

So I had an n-gram analyzer on some of the aggregated fields, but not on the original ones (because I did not want to query them directly).所以我对一些聚合字段有一个 n-gram 分析器,但对原始字段没有(因为我不想直接查询它们)。 Once I added the analyzer to the individual fields highlighting started working properly.一旦我将分析器添加到突出显示的各个字段,就会开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM