Elastica - 多个bool查询 - 子查询

时间:2016-03-14 01:28:42

标签: elasticsearch elastica

我想使用elastica来过滤以下类别中的多个匹配项:

类似的东西:

(categories.name =“category-1”AND categories.level = 0)AND(categories.name =“category-2”AND categories.level = 1)

Project   Start
-------   -----
Test1     May
Test2     April

我试过了:

Project  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
-------  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---
Test1                        .15  .20  .10  1.5
Test2                   .50  .35  .25  .15

1 个答案:

答案 0 :(得分:1)

刚刚将您的布尔子句转换为JSON格式,可以直接传递给elasticsearch查询。我认为这应该有用。

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "categories",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "categories.name": {
                        "value": "category-1"
                      }
                    }
                  },
                  {
                    "term": {
                      "categories.level": {
                        "value": "0"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "categories",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "categories.name": {
                        "value": "category-2"
                      }
                    }
                  },
                  {
                    "term": {
                      "categories.level": {
                        "value": "1"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
相关问题