Elasticsearch:仅返回突出显示的子字符串/关键字匹配

3

如果我要搜索完整字符串Knasweg 12, 9062 Knasweg, Österreich,如何突出显示(并返回)仅限于子字符串Knasweg

换句话说,我想查询以下内容:

GET _search
{
  "query": {
    "match": {
      "location.pretty_address": {
        "query": "Knasweg",
        "operator": "and",
        "fuzziness": 1
      }
    }
  },
  "highlight": {
    "pre_tags": "",
    "post_tags": "",
    "fields": {
      "location.pretty_address": {
        "highlight_query": {
          "bool": {
            "must": {
              "match": {
                "location.pretty_address": {
                  "query": "Knasweg"
                }
              }
            }
          }
        }
      }
    }
  }
}

返回

  "highlight": {
    "location.pretty_address": [
      "Knasweg"
    ]
  }

替代

  "highlight": {
    "location.pretty_address": [
      "Knasweg 12, 9062 Knasweg, Österreich"
    ]
  }    

我的映射:

"location": {
  "dynamic": "true",
  "properties": {
    "pretty_address": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      },
      "analyzer": "autocomplete_analyzer"
    }
}

我的设置:

"settings": {
  "index": {
    "analysis": {
      "analyzer": {
        "comma_analyzer": {
          "tokenizer": "comma_tokenizer"
        },
        "autocomplete_analyzer": {
          "filter": "lowercase",
          "tokenizer": "autocomplete_tokenizer"
        }
      },
      "tokenizer": {
        "autocomplete_tokenizer": {
          "type": "ngram",
          "min_gram": "3",
          "max_gram": "20"
        },
        "comma_tokenizer": {
          "pattern": ", ",
          "type": "pattern"
        }
      }
    }
  }
}
1个回答

2
根据此处的文档,您应该添加fragment_size参数,并将其设置为1,其中1是您查询中标记的数量。请注意保留HTML标签。
GET _search
{
  "query": {
    "match": {
      "location.pretty_address": {
        "query": "Knasweg",
        "operator": "and",
        "fuzziness": 1
      }
    }
  },
  "highlight": {
    "pre_tags": "",
    "post_tags": "",
    "fragment_size" : 1,
    "fields": {
      "location.pretty_address": {
        "highlight_query": {
          "bool": {
            "must": {
              "match": {
                "location.pretty_address": {
                  "query": "Knasweg"
                }
              }
            }
          }
        }
      }
    }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接