简体   繁体   English

基于elasticsearch中的term或bool查询匹配突出显示

[英]highlighting based on term or bool query match in elasticsearch

I have two queries. 我有两个问题。

{'bool':
    {'must':
        { 'terms': 'metadata.loc':['ten','twenty']}
        { 'terms':  'metadata.doc':['prince','queen']}
     }
     {'should':         
         { 'match': 'text':'kingdom of dreams'}
     }
},
{'highlight':
     {'text':
         {'type':fvh,
          'matched_fields':['metadata.doc','text']
         }
      }
 }

There are two questions ? 有两个问题?

  1. Why the documents with should query match are getting highlighted whereas documents with only must term match are not getting highlighted. 为什么带有应该查询匹配的文档会突出显示,而只有必须匹配的文档才会突出显示。

  2. Is there any way to mention highlight condition specific to term query above ? 有没有什么方法可以提到特定于以上术语查询的突出显示条件?

This means highlight condition for { 'terms': 'metadata.loc':['ten','twenty']} 这意味着突出{ 'terms': 'metadata.loc':['ten','twenty']}

and a seperate highlight condition for { 'terms': 'metadata.doc':['prince','queen']} { 'terms': 'metadata.doc':['prince','queen']}的单独突出显示条件

1) Only documents with should query are getting highlighted because you are highlighting against only text field which is basically your should clause. 1)只有具有应该查询的文档才会突出显示,因为您只针对text字段突出显示,而该text字段基本上是您的should子句。 Although you are using matched_fields , you are considering only text field. 虽然您使用的是matched_fields ,但您只考虑text字段。

From the Docs 来自Docs

All matched_fields must have term_vector set to with_positions_offsets but only the field to which the matches are combined is loaded so only that field would benefit from having store set to yes. 所有matched_fields必须将term_vector设置为with_positions_offsets,但只加载组合匹配的字段,因此只有该字段才能将store设置为yes。

Also you are combining two very different fields, 'matched_fields':['metadata.doc','text'] , this is hard to understand, again from the Docs 此外,您正在组合两个非常不同的字段, 'matched_fields':['metadata.doc','text'] ,这很难理解,再次来自文档

Technically it is also fine to add fields to matched_fields that don't share the same underlying string as the field to which the matches are combined. 从技术上讲,将字段添加到不与共享匹配的字段共享相同底层字符串的matched_fields也是可以的。 The results might not make much sense and if one of the matches is off the end of the text then the whole query will fail. 结果可能没有多大意义,如果其中一个匹配项不在文本的末尾,则整个查询将失败。

2) You can write highlight condition specific to term query with Highlight Query 2)您可以使用突出显示查询编写特定于术语查询的突出显示条件

Try this in your highlight part of the query 在查询的highlight部分中尝试此操作

{
  "query": {
    ...your query...
  },
  "highlight": {
    "fields": {
      "text": {
        "type": "fvh",
        "matched_fields": [
          "text",
          "metadata.doc"
        ]
      },
      "metadata.doc": {
        "highlight_query": {
          "terms": {
            "metadata.doc": [
              "prince",
              "queen"
            ]
          }
        }
      },
      "metadata.loc": {
        "highlight_query": {
          "terms": {
            "metadata.loc": [
              "ten",
              "twenty"
            ]
          }
        }
      }
    }
  }
}

Does this help? 这有帮助吗?

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

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