如何在elasticsearch中的_id上进行通配符或正则表达式匹配?

abi*_*964 10 elasticsearch

从下面的示例elasticsearch数据我想应用通配符说明*.000ANT.*,_id以便获取其_id包含的所有文档000ANT.请帮忙.

"hits": [
  {
    "_index": "data_collector",
    "_type": "agents",
    "_id": "Org000LAN_example1.com",
    "_score": 1,
    "fields": {
      "host": [
        "example1.com"
      ]
    }
  },
  {
    "_index": "data_collector",
    "_type": "agents",
    "_id": "000BAN_example2.com",
    "_score": 1,
    "fields": {
      "host": [
        "example2.com"
      ]
    }
  },
  {
    "_index": "data_collector",
    "_type": "agents",
    "_id": "000ANT_example3.com",
    "_score": 1,
    "fields": {
      "host": [
        "example3.com"
      ]
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

mid*_*ido 8

这只是 Andrei Stefan答案的扩展

{
  "query": {
    "script": {
      "script": "doc['_id'][0].indexOf('000ANT') > -1"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:我不知道这样的查询对性能的影响,很可能这是一个坏主意。谨慎使用并尽可能避免。


Val*_*Val 6

您可以使用这样的通配符查询,但值得注意的是,不建议以通配符开头,*因为性能会受到影响。

{
  "query": {
    "wildcard": {
      "_uid": "*000ANT*"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

另请注意,如果您要搜索的通配符术语与文档的类型名称匹配,则 usinguid将不起作用,因为 uid 只是类型和 id 的缩写:type#id


小智 5

尝试这个

{
   "filter": {
      "bool": {
         "must": [
            {
               "regexp": {
                  "_uid": {
                     "value": ".*000ANT.*"
                  }
               }
            }
         ]
      }
   }
}
Run Code Online (Sandbox Code Playgroud)


And*_*fan 5

允许对 id 的映射建立索引:

{
  "mappings": {
    "agents": {
        "_id": {
        "index": "not_analyzed"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

并使用 aquery_string来搜索它:

{
  "query": {
    "query_string": {
      "query": "_id:(*000ANT*)",
      "lowercase_expanded_terms": false
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

或者像这样(使用脚本并且仍然只查询_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')"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)