Aviran
Aviran

Reputation: 5480

Filter on ID values

Im experiencing an issue when filtering a field using a value of IDs, not sure how that can happen, here's the mapping and an example.

As can be seen after adding a new user, with the primary_user set to AWBFyulclsp0PJbvPiuB it will not be found using filter unless keyword is used in the filter request. This only happens with values of IDs. What's the root cause for this behavior?

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

Upvotes: 0

Views: 50

Answers (1)

Lupanoide
Lupanoide

Reputation: 3222

Because primary_user has a text datatype, as written in your mappings:

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

So or you change the field to query against:

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

or change from term to match query:

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

Upvotes: 2

Related Questions