如同磁铁吸引四周的铁粉,热情也能吸引周围的人,改变周围的情况。

在一个query里能否同时限制某个object/nested里多个字段的值?

Elasticsearch | 作者 God_lockin | 发布于2019年05月28日 | 阅读数:2150

想返回同时满足一下条件的数据:
conditions.id = 'stock' && conditions.num = 0
conditions.id = 'news' && conditions.num = 2
 这是mapping
{
"service": {
"type": "keyword"
},
"priority": {
"type": "integer"
},
"order": {
"properties": {
"order": {
"type": "integer"
},
"card": {
"type": "keyword"
}
}
},
"id": {
"type": "keyword"
},
"description": {
"type": "text",
"index": false
},
"conditions": {
"properties": {
"num": {
"type": "integer_range"
},
"id": {
"type": "keyword"
}
}
}
}
这是示例数据
{
"id": 1,
"desc": "aabb",
"priority": 0,
"conditions": [
{
"id": "stock",
"num": {
"gte": 1
}
},
{
"id": "label",
"num": {
"gte": 0,
"lte":0
}
}
],
"service": [
"news",
"stock"
],
"order": [
{
"card": "stock_card1",
"order": 2
},
{
"card": "news",
"order": 1
}
]
}
{
  "id": 1,
  "desc": "aabb",
  "priority": 0,
  "conditions": [
    {
      "id": "stock",
      "num": {
        "gte": 0,
        "lte": 0
      }
    },
    {
      "id": "label",
      "num": {
        "gte": 1
      }
    }
  ],
  "service": [
    "stock",
    "news"
  ],
  "order": [
    {
      "card": "stock_card1",
      "order": 1
    },
    {
      "card": "news",
      "order": 2
    }
  ]
}
然后我写的DSL返回不了想要的结果,这样写的结果就是直接俩文档都返回了
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": {
"conditions.num": 0
}
},
{
"term": {
"conditions.id": "stock"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"conditions.num": 2
}
},
{
"term": {
"conditions.id": "label"
}
}
]
}
}
]
}
}
}
已邀请:

God_lockin

赞同来自:

把conditions的mapping改成了nested,然后改了下query,
结果目前是可以满足的,但是用query_string不是很方便写在代码里动态生成,有没有大佬有更好的方式可以的解决的?
mapping:
{
"service": {
"type": "keyword"
},
"priority": {
"type": "integer"
},
"order": {
"properties": {
"order": {
"type": "integer"
},
"card": {
"type": "keyword"
}
}
},
"id": {
"type": "keyword"
},
"description": {
"type": "text",
"index": false
},
"conditions": {
[u][b] "type":"nested",[/b][/u]
"properties": {
"num": {
"type": "integer_range"
},
"id": {
"type": "keyword"
}
}
}
}
然后用俩嵌套的query string来限定条件
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "conditions",
"query": {
"query_string": {
"query": "conditions.id:stock AND conditions.num:1"
}
}
}
},
{
"nested": {
"path": "conditions",
"query": {
"query_string": {
"query": "conditions.id:label AND conditions.num:0"
}
}
}
}
]
}
}
}
结果目前是可以满足的
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 3.3862944,
"hits": [
{
"_index": "testindex1",
"_type": "_doc",
"_id": "z68W_WoBIQiEJvYCTExe",
"_score": 3.3862944,
"_source": {
"id": 1,
"desc": "aabb",
"priority": 0,
"conditions": [
{
"id": "stock",
"num": {
"gte": 1
}
},
{
"id": "label",
"num": {
"gte": 0,
"lte": 0
}
}
],
"service": [
"news",
"stock"
],
"order": [
{
"card": "stock_card1",
"order": 2
},
{
"card": "news",
"order": 1
}
]
}
}
]
}
}

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

inner_hits了解下,看能否满足你的需求。

要回复问题请先登录注册