【问题标题】:Elasticsearch- highlighting on both ".keyword" and text fieldsElasticsearch-在“.keyword”和文本字段上突出显示
【发布时间】:2018-11-19 07:40:10
【问题描述】:

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

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

我的自定义分析器是:

"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>"
  ]
}

【问题讨论】:

  • 好的,你期望实现什么?
  • 我想知道当文本字段已经被点击时是否有办法忽略 .keyword 文件?在上述情况下,高亮存在冗余,因为同一字段被高亮两次。

标签: elasticsearch


【解决方案1】:

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

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

【讨论】:

  • 可以使这种高亮变化通用吗?因为,我有大约 100 个这样的字段,我认为在亮点中提及所有 100 个字段不是正确的方式
  • 恐怕无法以更通用的方式做到这一点。
猜你喜欢
  • 2013-05-22
  • 2015-03-30
  • 2013-08-24
  • 2011-06-26
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
相关资源
最近更新 更多