Query on _id field

I had some trouble trying to query the id_ field

The following query works:

GET my_index/my_type/_search
{
   "query" : {
     "match":{
        "_id": "ABC_my_content1"
     }
   }
}

But what if I want to search for all the document that has the _id start with ABC?
This does not work:

GET my_indxe/my_type/_search
{
   "query" :{
     "query_string" : {
         "default_field" : "_id",
         "query" : "ABC*"
      }
   }
}

I got the error
Can only use prefix queries on keyword and text fields - not on [_id] which is of type [_id]

Any suggestions?

2 Likes

Yeah, so the _id field is sorta special. It's not really an indexed field like other text fields, it's actually "generated" based on the UID of the document (see: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html).

So it only supports a limited subset of the query functionality. If you want to do more advanced text search like prefix/wildcard/fuzzy, etc, you'll need to index the ID into an actual text field on the document itself.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.