使用netstat -lntp来看看有侦听在网络某端口的进程。当然,也可以使用 lsof。

Nested 存储对象型数组,怎么筛选满足多个key对应多个value

Elasticsearch | 作者 night | 发布于2018年06月12日 | 阅读数:3579

公司最近项目,需要用ES搜索商品 
 
一个商品包含多个属性,我的mapping如下(去掉了无用字段)
商品中只有id、和属性两个字段,属性中包含属性id,属性名称,属性值id,属性值名称
put /goods_index
{
    "mappings":{
        "goods_type":{
            "properties":{
                "id":{
                    "type":"keyword",
                    "store":"true"
                },
                "attrs":{
                    "type":"nested",
                    "properties":{
                        "attr_name":{
                            "type":"keyword",
                            "index":true
                        },
                        "attr_value":{
                            "type":"keyword",
                            "index":true
                        },
                        "attr_id":{
                            "type":"keyword",
                            "index":true
                        },
                        "attr_value_id":{
                            "type":"keyword",
                            "index":true
                        }
                    }
                }
            }
        }
    }
}

 
属性用了nested类型,商品数据如下:
put /goods_index/goods_type/1
{
    "id":"1",
    "attrs":[
        {
            "attr_name":"颜色",
            "attr_value":"黄色",
            "attr_id":"1",
            "attr_value_id":"11"
        },
        {
            "attr_name":"操作系统",
            "attr_value":"Mac OS",
            "attr_id":"2",
            "attr_value_id":"20"
        }
    ]
}

 

put /goods_index/goods_type/2
{
    "id":"2",
    "attrs":[
        {
            "attr_name":"颜色",
            "attr_value":"蓝色",
            "attr_id":"1",
            "attr_value_id":"12"
        },
        {
            "attr_name":"操作系统",
            "attr_value":"Mac OS",
            "attr_id":"2",
            "attr_value_id":"20"
        }
    ]
}
现在我想筛选出颜色为蓝色(attr_id=1,attr_value_id=12),并且操作系统为Mac OS(attr_id=2,attr_value_id=20)的商品,查询该怎么写?

 
之前我用了nested套用bool  -- must  的方法查询,但是结果为空,后来一想,确实must attrs.attr_id=1,attrs.attr_id=2 这两个条件在一起肯定查不出数据
 
get  /goods_index/goods_type/_search
{
  "query" : {
    "nested" : {
      "path" : "attrs",
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "attrs.attr_id": {
                        "value": "1"
                      }
                    }
                  },
                  {
                    "term": {
                      "attrs.attr_value_id": {
                        "value": "12"
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "attrs.attr_id": {
                        "value": "2"
                      }
                    }
                  },
                  {
                    "term": {
                      "attrs.attr_value_id": {
                        "value": "20"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

 
后来又用了should连接,但是商品1也被查了出来,请问高手们有没有写过类似这种查询的?
 
ps:版本 6.2.4
 
 

 
已邀请:

strglee

赞同来自: night

GET goods_index/goods_type/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attr_id": {
"value": "1"
}
}
},
{
"term": {
"attrs.attr_value_id": {
"value": "12"
}
}
}
]
}
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attr_id": {
"value": "2"
}
}
},
{
"term": {
"attrs.attr_value_id": {
"value": "20"
}
}
}
]
}
}
}
}
]
}
}
}

要回复问题请先登录注册