How to do a wildcard or regex match on _id in elasticsearch?

前端 未结 5 519
孤城傲影
孤城傲影 2021-01-01 23:25

From below sample elasticsearch data I want to apply wildcard say *.000ANT.* on _id so as to fetch all docs whose _id contains 0

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-02 00:19

    Allow your mapping for the id to be indexed:

    {
      "mappings": {
        "agents": {
            "_id": {
            "index": "not_analyzed"
          }
        }
      }
    }
    

    And use a query_string to search for it:

    {
      "query": {
        "query_string": {
          "query": "_id:(*000ANT*)",
          "lowercase_expanded_terms": false
        }
      }
    }
    

    Or like this (with scripts and still querying only the _id):

    {
      "query": {
        "filtered": {
          "filter": {
            "script": {
              "script": "org.elasticsearch.index.mapper.Uid.splitUidIntoTypeAndId(new org.apache.lucene.util.BytesRef(doc['_uid'].value))[1].utf8ToString().contains('000ANT')"
            }
          }
        }
      }
    }
    

提交回复
热议问题