【问题标题】:Is there a way to exclude a particular term from elastic search highlights?有没有办法从弹性搜索亮点中排除特定术语?
【发布时间】:2021-03-31 16:57:03
【问题描述】:

我正在弹性搜索(6.0 版)中尝试一个查询,其中我有一个基本查询,最重要的是,我应用了过滤器来缩小搜索范围。如下:

GET target_index/_search
{
  "from": {start},
  "size": {offset},
  "_source": [
    "id",
    "name",
    "email",
    "company",
    "created_at",
  ],
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "filter": [
            { "terms":{"name.raw": ["test","test2"] }},
            { "terms":{"email.raw": ["test@test.com","test2@test.com"] }} 
          ]
        }
      },
      "must": {
        "query_string": {
          "query": "test",
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "*":{
        "type":"plain"
      }
    }
  }
}

当前结果-

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.90374,
    "hits": [
      {
        "_index": "index_name",
        "_id": "my_id",
        "_score": 1.90374,
        "_source": {
          "id": 2,
          "name": "test", 
          "email": "test@test.com",
          "company": "test company"
        },
        "highlight": {
          "name.raw": [
            "<em>test</em>"
          ],
          "name": [
            "<em>test</em>"
          ],
          "company": [
            "<em>test</em> company"
          ]
        }
      }
    ]
  }
}

想要的结果 -

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.90374,
    "hits": [
      {
        "_index": "index_name",
        "_id": "my_id",
        "_score": 1.90374,
        "_source": {
          "id": 2,
          "name": "test", 
          "email": "test@test.com",
          "company": "test company"
        },
        "highlight": {
          "company": [
            "<em>test</em> company"
          ]
        }
      }
    ]
  }
}

在这里,在所需结果的突出显示中,我不想要“name”和“name.raw”的数据。不应仅针对此特定查询搜索此字段,因此我无法完全禁用该字段的搜索。

我有很多术语,无法指定要包含在查询中的每个术语。有没有办法只从查询搜索中排除几个字段?

相关的 ES 文档 - https://www.elastic.co/guide/en/elasticsearch/reference/6.0/index.html

【问题讨论】:

  • 您能指定文档、当前响应和所需响应吗?
  • @JoeSorocin 用相关数据编辑了问题。如果您还需要什么,请告诉我

标签: elasticsearch


【解决方案1】:

除了排除某些字段,您可以只包含您需要的那些:

{
  "query": {
    ...
  },
  "highlight": {
    "fields": {
      "company":{          <---
        "type":"plain"
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多