【问题标题】:Highlight gives weird results突出显示奇怪的结果
【发布时间】:2018-05-28 09:15:42
【问题描述】:

我正在使用 elasticsearch,但我的亮点并没有达到我的预期。我的设置是这样的:

PUT my_index
{
  "settings": {
       "analysis": {
            "analyzer": {
                 "my_analyzer": {
                      "tokenizer": "my_tokenizer",
                      "filter": {
                           'lowercase','asciifolding'
                      }
                 }
            },
            "tokenizer": {
                 "my_tokenizer": {
                      "type": "ngram",
                      "min_gram": 2,
                      "max_gram": 25,
                      "token_chars": [
                           "letter",
                           "digit"
                      ]
                 }
            }
       }
  }
}

我将一些产品放入我的索引中

PUT index/product/1
{
 "name" : "Kit Guirlande Guinguette 50m Transparent",
 "field2": "foo"
}

PUT index/product/2
{
 "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
  "field2": "foo"
}

namefield2 的映射:

"name_product": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        },
        "search": {
            "type": "text",
            "analyzer": "my_analyzer",
            "search_analyzer": "standard"
        }
    },
    "analyzer": "my_analyzer"
},
"fields2": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }
     },
     "analyzer": "my_analyzer"
},

我正在做一项研究:

GET index/product/_search
{
 "query":{
      "multi_match": {
           "query" : "guirlande gui"
           "fields":[
                'name','field2'
           ]
          "minimum_should_match" : "100%"
      }
 }
 "highlight" : {
      "fields":{
          "name.search" : {
               'highlight_query':{
                    'match':{
                         'query'=>"guirlande gui"
                    }
               }
          }
      }
 }
}

回应:

{
 "hits": {
  "total": 2,
   "hits": [
         {
               "_index":"index",
               "_type": "product",
               "_id": "1",
               "_source": {
                     "name": "Guirlande Guinguette Blanc 20 Bulbes 10M"
                },
               "highlight": {
                     "name.search": [
                           " <em>Guirlande<em> Guinguette Blanc 20 Bulbes 10M"
                     ]
               }
         },

         {
               "_index": "index",
               "_type": "product",
               "_id": "2",
               "_source": {
                     "name": "Kit Guirlande Guinguette 30m Blanche"
                },
               "highlight": {
                     "name.search": [
                           " Kit Guirlande Guinguette 30m Blanche"
                     ]
               }
         }
   ]
 }
}

但是对于第二个亮点,我想拥有" Kit &lt;em&gt;Guirlande Gui&lt;/em&gt;nguette 30m Blanche"。我认为当匹配部分不在开始但无法弄清楚原因时,我会遇到这种问题。

编辑: 我还尝试将突出显示的类型更改为“统一”,它更好,但仍然不是我想要的。它给了我:

{
 "hits": {
  "total": 2,
   "hits": [
         {
               "_index":"index",
               "_type": "product",
               "_id": "1",
               "_source": {
                     "name": "Guirlande Guinguette Blanc 20 Bulbes 10M"
                },
               "highlight": {
                     "name": [
                           " <em>Guirlande Gui</em>nguette Blanc 20 Bulbes 10M"
                     ]
               }
         },

         {
               "_index": "index",
               "_type": "product",
               "_id": "2",
               "_source": {
                     "name": "Kit Guirlande Guinguette 30m Blanche"
                },
               "highlight": {
                     "name": [
                           " Kit<span class="highlight"> G</span><span class="highlight">u</span><span class="highlight">i</span><span class="highlight">r</span><span class="highlight">l</span><span class="highlight">a</span><span class="highlight">n</span><span class="highlight">d</span><span class="highlight">e</span><span class="highlight"> </span><span class="highlight">G</span><span class="highlight">u</span><span class="highlight">i</span>n<span class="highlight">gu</span>ett<span class="highlight">e </span>30m B<span class="highlight">la</span><span class="highlight">n</span>che"
                     ]
               }
         }
   ]
 }
 }

所以它不是真的可读,所以我认为一张图片可能会有所帮助:

我们可以看到我在高亮中的想法是正确的,但我在高亮中也有很多不需要的信息,例如“blanche”中的“lan”和“e”或“Guinguette”的第二个“gu”

映射:

分析器:

搜索:

【问题讨论】:

  • 您的测试数据完全错误:您只添加了设置,我们不知道name字段的映射是什么,foo字段也是如此。为能够帮助您的人提供有效的测试。到目前为止,您提供的内容,即使稍作修改,也不会返回任何文档。
  • @AndreiStefan 这样更好吗?
  • 我仍然认为您的测试不是您实际使用的测试。您拥有的分析器没有将文本小写,但在您的搜索中使用小写guirlande gui,这与您作为文档放置的内容完全不匹配。我可以尝试猜测你想要达到的目标,如果我给你一个解决方案,你会回来说在任何情况下它都不起作用等等。
  • 在我看来,您实际用于发布结果的分析器不是您在本文中定义和呈现的分析器。您的 my_analyzer 没有像 guirlande gui 那样拆分文本,因此可以像这样突出显示。
  • 我在分析器中使用了小写但忘记添加它。

标签: elasticsearch highlight


【解决方案1】:

所以它现在可以工作了。这是我的最终配置:

DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer",
          "filter": ["lowercase"]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 25,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "product": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            },
            "search": {
              "type": "text",
              "search_analyzer": "standard",
              "analyzer": "my_analyzer"
            }
          }
        }
      }
    }
  }
}

PUT my_index/product/1
{
 "name" : "Kit Guirlande Guinguette 50m Transparent",
 "field2": "foo"
}

PUT my_index/product/2
{
 "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
  "field2": "foo"
}

GET my_index/product/_search
{
  "query": {
    "multi_match": {
      "query": "Guirlande Gui",
      "fields": [
        "name.search",
        "field2"
      ],
      "minimum_should_match": "100%"
    }
  },
  "highlight": {
    "fields": {
      "name.search": {
        "highlight_query": {
          "match": {
            "name.search": {
              "query": "Guirlande Gui"
            }
          }
        }
      }
    },
    "type" : "unified"
  }
}

【讨论】:

    【解决方案2】:

    在我看来,您的分析器应该是这样的,而且您也可以查询:

    • 它应该有一个lowercase 过滤器
    • 它应该有一个名为 search 的第二个子字段(或您选择的名称),它应该有一个不同的搜索分析器和相同的索引分析器
    • search 子字段应该是用于突出显示部分的子字段,但 highlight_query 不同。
    DELETE my_index
    PUT my_index
    {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "my_tokenizer",
              "filter": ["lowercase"]
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "type": "ngram",
              "min_gram": 2,
              "max_gram": 25,
              "token_chars": [
                "letter",
                "digit"
              ]
            }
          }
        }
      },
      "mappings": {
        "product": {
          "properties": {
            "name": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                },
                "search": {
                  "type": "text",
                  "search_analyzer": "standard",
                  "analyzer": "my_analyzer"
                }
              },
              "analyzer": "my_analyzer"
            }
          }
        }
      }
    }
    
    PUT my_index/product/1
    {
     "name" : "Kit Guirlande Guinguette 50m Transparent",
     "field2": "foo"
    }
    
    PUT my_index/product/2
    {
     "name": "Guirlande Guinguette Blanc 20 Bulbes 10M",
      "field2": "foo"
    }
    
    GET my_index/product/_search
    {
      "query": {
        "multi_match": {
          "query": "Guirlande Gui",
          "fields": [
            "name",
            "field2"
          ],
          "minimum_should_match": "100%"
        }
      },
      "highlight": {
        "fields": {
          "name.search": {
            "highlight_query": {
              "match": {
                "name.search": {
                  "query": "Guirlande Gui"
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我按照你说的做了,但还是有问题。现在我有&lt;em&gt;Guirlande&lt;/em&gt; Guinguette 10M Blanche Avec Prise 和一个从别的东西开始的人,而不是 Guirlande 保持不变。这里Kit Guirlande Guinguette 30m Blanche。我用你的想法更新了我的帖子
    • 我还添加了我的代码图片,以确保我没有犯愚蠢的错误
    • 很抱歉,但它对我有用。我使用了 ES 5.6.3,这是我完整的测试用例:gist.github.com/astefan/0aa1ec9a09738e8f61679bb740d7de9d。我建议您使用单独的小索引执行一个隔离的测试用例,并提供您用来测试它的命令的分步列表。我建议您像我一样在要点中发布您的 REST 命令。我可以测试这些并重现,否则我不知道如何进一步帮助您。
    • 好的,感谢您的宝贵时间。我会尝试从头开始重做 evrtyhing,如果可行,我明天会验证你的答案
    • 我找到了解决方案。你帮助了很多,所以我会给出答案,然后给你赏金。谢谢朋友!
    猜你喜欢
    • 1970-01-01
    • 2016-10-04
    • 2011-08-19
    • 2015-01-13
    • 2016-11-19
    • 2012-07-23
    • 2013-06-27
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多