Py学习  »  Elasticsearch

ElasticSearch-突出显示“.keyword”和文本字段

M Nikesh • 4 年前 • 410 次点击  

在使用完整数据搜索字段时,我发现了突出显示的问题。我使用了自定义分析器,每个字段都存储为文本和关键字。

我使用空白作为搜索分析器。

我的自定义分析器是:

"analysis": {
  "filter": {
    "indexFilter": {
      "type": "pattern_capture",
      "preserve_original": "true",
      "patterns": [
        "([@,$,%,&,!,.,#,^,*]+)",
        "([\\w,.]+)",
        "([\\w,@]+)",
        "([-]+)",
        "(\\w+)"
      ]
    }
  },
  "analyzer": {
    "indexAnalyzer": {
      "filter": [
        "indexFilter",
        "lowercase"
      ],
      "tokenizer": "whitespace"
    },
    "searchAnalyzer": {
      "filter": [
        "lowercase"
      ],
    "tokenizer": "whitespace"
  }
}

我的映射文件是:

"field": {
  "type": "text",
  "term_vector": "with_positions_offsets",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  },
  "analyzer": "indexAnalyzer",
  "search_analyzer": "searchAnalyzer"
}

我的问题是:

{
  "from": 0,
  "size": 24,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "monkey business",
            "type": "phrase",
            "slop": "2",
            "fields": []
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "highlight": {
    "type": "unified",
    "fields": {
      "*": {}
    }
  }
}

我的亮点是:

"highlight": {
  "field.keyword": [
    "<em>monkey business</em>"
  ],
  "field": [
    "<em>monkey</em> <em>business</em>"
  ]
}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40019
 
410 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Piotr Pradzynski
Reply   •   1 楼
Piotr Pradzynski    5 年前

我可以建议您这样的查询(分析和映射保持不变):

GET /index-53370229/_doc/_search
{
  "from": 0,
  "size": 24,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "monkey business",
            "type": "phrase",
            "slop": "2",
            "fields": []
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "highlight": {
    "type": "fvh",
    "fields": {
      "field": {
        "matched_fields": [
          "field",
          "field.keyword"
        ]
      }
    }
  }
}

唯一的变化是 highlight 章节。因此,您将得到:

"highlight": {
  "field": [
    "<em>monkey business</em>"
  ]
}

我用过 matched_fields 属性,您可以在文档中阅读该属性: https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-request-highlighting.html#matched-fields