EasticSearch查询特定类型的条件?

时间:2018-01-18 19:39:28

标签: elasticsearch

我正在尝试编写一个查询来执行以下操作:

例如:CAR索引具有以下映射:

ford
    color
    model
acura
    color
    model
toyota
    color
    model
    toyota_type

我想编写一个可以搜索所有文档的查询 color =“blue”,和toyota_type =“a”|| “c”仅限于类型=“toyota”

棘手的部分是如果我写这样的查询,它只显示TOYOTA类型的结果,但不匹配其他类型的文档:

Looking for: 
     ("color is blue" AND type is not toyota) OR 
     ("color is blue" AND type IS toyota AND toyota_type = 'a' or 'c')
In other words, search ALL documents; 
(1) IF type == toyota (toyota_type == 'a' or 'c' ) AND color == "blue"
(2) IF type != toyota  color == "blue"

{
      "bool": {
        "must": [{
          "term": {
            "color": "blue"
          }
        }, {
          "bool": {
            "should": {
              "terms": {
                "toyota_type": ["a","c"]
              }
            }
          }
        }]
      }
    }

edit: try this and also didn't work. This returns toyota_type != ('a' or 'c')

    {
        "query":{
            "bool": {
            "must": [{
              "term": {
                "color": 
              }
            }, {
              "bool": {
                "must_not": [{
                  "term": {
                    "color": "red"
                  }
                }]
              }
            }], 
            "should":{
                "terms":{
                    "toyota_type": ['a','c']
                }
            }
          }
        }
    }

1 个答案:

答案 0 :(得分:1)

您在顶级bool查询中使用must,这是对elasticsearch的AND查询。因此,查询可以转换为查找所有结果,其中"颜色为蓝色" AND" toyota_type是' a'或者' c'"。但是,据我所知,你需要的是对此的查询:"颜色为蓝色" OR" toyota_type是' a'或者' c'"。如果这是要求,您应该进行此查询。

{
  "bool": {
    "should": [
    {
      "term": {
        "color": "blue"
      }
    },
    {
        "terms": {
            "toyota_type": ["a","c"]
        }
    }
   ]
  }
}

对于新的编辑案例,查询如下:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "color": {
              "value": "blue"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must_not": [
                    {
                      "term": {
                        "type": {
                          "value": "toyota"
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "type": {
                          "value": "toyota"
                        }
                      }
                    },
                    {
                      "terms": {
                        "toyota_type": [
                          "a",
                          "c"
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
相关问题