Py学习  »  Elasticsearch

ElasticSearch数组至少包含一个元素

Wide Brive • 4 年前 • 625 次点击  

我想用ElasticSearch按标签搜索食谱

{
  ...
  "tag": [
      "cool",
      "cooler"
  ]
},
{
  ...
  "tag": [
      "cool",
      "hard"
  ]
},
{
  ...
  "tag": [
      "coolest",
      "hardest"
  ]
},

我要搜索包含“酷”标签的所有实体

我尝试了什么:

GET /recipes/_search
{
  "query": {
    "terms": {
      "tag": ["cool"]
    }
  }
}

什么也不返回

GET /recipes/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tag": "cool"
          }
        }
      ]
    }
  }
}

什么也不返回

GET /recipes/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "tag": "cool"
          }
        }
      ]
    }
  }
}

返回所有实体

如何只检索包含“cool”和可能包含其他值的项,但规则是标记必须存在于标记数组中

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38296
 
625 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Tarek Essam
Reply   •   1 楼
Tarek Essam    5 年前

此解决方案使用脚本查询 script query

GET /recipes/_search
{
  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "source": "doc['tag'] instanceof List && doc['tag'][0].contains('cool')"
          }
        }
      }
    }
  }
}
JeyanthiLal
Reply   •   2 楼
JeyanthiLal    5 年前
GET /recipes/_search
{
   "query": {
      "bool":{
         "should":[
            {"wildcard" : { "tag" : "cool*" } }
            ]

     }
}