过滤ID值

时间:2018-03-12 12:57:19

标签: elasticsearch elasticsearch-5

我在使用ID值过滤字段时遇到问题,不知道如何发生这种情况,这里是映射和示例。

在添加新用户后可以看到,primary_user设置为AWBFyulclsp0PJbvPiuB,除非在过滤请求中使用filter,否则无法使用keyword找到。 这仅适用于ID值。 这种行为的根本原因是什么?

   GET users/_mapping/

    Response:
    {
     ...
     ...
     "primary_user": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
    }
------
    POST users/user
    {
      "primary_user": "AWBFyulclsp0PJbvPiuB"
    }

    Response:
    {
      "_index": "users",
      "_type": "user",
      "_id": "AWIaOFgQmedTIIkbtGPG",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": true
    }
------
    GET users/user/_search
    {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "primary_user": "AWBFyulclsp0PJbvPiuB"
            }
          }
        }
      }
    }
    Response:
    {
      "took": 0,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
      }
}
------
    GET users/user/_search
    {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "primary_user.keyword": "AWBFyulclsp0PJbvPiuB"
            }
          }
        }
      }
    }

    Response:
    {
        "_index": "users",
        "_type": "user",
        "_id": "AWIaOFgQmedTIIkbtGPG",
        "_score": 0,
        "_source": {
          "primary_user": "AWBFyulclsp0PJbvPiuB"
        }
      }

1 个答案:

答案 0 :(得分:2)

因为primary_user具有文本数据类型,如在映射中所写:

 "primary_user": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }

所以或者您更改要查询的字段:

GET users/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "primary_user.keyword": "AWBFyulclsp0PJbvPiuB"
        }
      }
    }
  }
}

或从术语更改为匹配查询:

GET users/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "match": {
          "primary_user": "AWBFyulclsp0PJbvPiuB"
        }
      }
    }
  }
}