【问题标题】:highlighting based on term or bool query match in elasticsearch基于弹性搜索中的术语或布尔查询匹配突出显示
【发布时间】:2016-03-10 07:33:10
【问题描述】:

我有两个问题。

{'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']
         }
      }
 }

有两个问题?

  1. 为什么带有 should query match 的文档被突出显示,而只有 must term match 的文档没有被突出显示。

  2. 有没有办法提到上面的术语查询特定的突出显示条件?

这意味着{ 'terms': 'metadata.loc':['ten','twenty']}的高亮条件

以及{ 'terms': 'metadata.doc':['prince','queen']} 的单独突出显示条件

【问题讨论】:

    标签: python search elasticsearch pyelasticsearch


    【解决方案1】:

    1) 只有带有 should 查询的文档才会突出显示,因为您只突出显示 text 字段,这基本上是您的 should 子句。尽管您使用的是 matched_fields ,但您只考虑了 text 字段。

    来自Docs

    所有matched_fields 必须将term_vector 设置为with_positions_offsets,但仅加载匹配项组合到的字段,因此只有该字段才能受益于将store 设置为yes。

    您还结合了两个非常不同的领域,'matched_fields':['metadata.doc','text'],这很难理解,再次来自文档

    从技术上讲,将字段添加到matched_fields 也很好,这些字段与匹配项组合到的字段不共享相同的基础字符串。结果可能没有多大意义,如果其中一个匹配项不在文本末尾,则整个查询将失败。

    2) 您可以使用Highlight 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"
                ]
              }
            }
          }
        }
      }
    }
    

    这有帮助吗?

    【讨论】:

    • chintan....{'highlight': {'text': {'type':fvh, 'matched_fields':['metadata.doc','text'] } } }.. . 这应该突出必须在文本字段中查询metadata.doc内容的匹配吗?
    • 我认为 metadata.doc 值需要在 text 字段中才能突出显示,所以 princequeen 应该是text,这两个字段的 analyzed 也不同,正如引用的文档,将它们分组可能没有意义。
    猜你喜欢
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多