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

问题描述 投票:2回答:1

给出完整的字符串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"
        }
      }
    }
  }
}
elasticsearch full-text-search elasticsearch-dsl
1个回答
0
投票
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" } } } } } } } } }
© www.soinside.com 2019 - 2024. All rights reserved.