elasticsearch:关键字数据类型字段与值数组的精确匹配

pobjuy32  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(435)

在elasticsearch中,我有一个电子邮件字段和标题字段的Map,如下所示:

{
  "person": {
    "mappings": {
      "_doc": {
         "email": {
           "type": "keyword",
           "boost": 80
          },
          "title": {
          "type": "text",
           "boost": 70
          }
       }
     }
   }

每个人可以有一个以上的电子邮件地址和标题。所以,我将值存储在数组中。我使用查询字符串搜索具有电子邮件地址和/或标题的人员。电子邮件地址必须完全匹配。
我用以下数据为一个文档编制了索引。打电话 GET person/_search 在kibana将产生以下文件中的结果。

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "person",
        "_type": "_doc",
        "_id": "101",
        "_score": 1,
        "_source": {
          "title": """["Actor", "Hero", "Model"]""",
          "email": """["jdepp@hotmail.com", "johnny@hollywood.com", "jdepp@gmail.com", "johnny.depp@yahoo.com"]""",
          "SEARCH_ENTITY": "PERSON"
        }
      }
    ]
  }
}

现在,当我添加一些电子邮件搜索参数时,结果中不会返回文档。记住电子邮件的类型是关键字。
请求:

GET person/_search
{
  "query" : {
    "query_string" : {
      "query" : "SEARCH_ENTITY:PERSON AND (email: (johnny.depp@yahoo.com))"
    }
  }
}

答复:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

但对于文本类型的标题字段,同样的查询也适用。
请求:

GET person/_search
{
  "query" : {
    "query_string" : {
      "query" : "SEARCH_ENTITY:PERSON AND (title: ((actor)))"
    }
  }
}

答复:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 20.137747,
    "hits": [
      {
        "_index": "person",
        "_type": "_doc",
        "_id": "101",
        "_score": 20.137747,
        "_source": {
           "ID": "101",
           "title": """["Actor", "Hero", "Model"]""",
           "email": """["jdepp@hotmail.com", "johnny@hollywood.com", "jdepp@gmail.com", "johnny.depp@yahoo.com"]"""
         }
      }
    ]
  }
}

有人能告诉我我需要做什么,使这个电子邮件字段是关键字类型的工作?
注意:如果我只存储一个电子邮件地址而不使用数组,它就可以正常工作。
谢谢。

q3aa0525

q3aa05251#

确保在中解析json数组字符串 title 以及 email 在索引文档之前:

POST person/_doc/101
{
  "title": [
    "Actor",
    "Hero",
    "Model"
  ],
  "email": [
    "jdepp@hotmail.com",
    "johnny@hollywood.com",
    "jdepp@gmail.com",
    "johnny.depp@yahoo.com"
  ],
  "SEARCH_ENTITY": "PERSON"
}

关于Map,不需要做任何更改——只需要字段值。

相关问题