Elastica - 多个 bool 查询 - 子查询

Elastica - multiple bool queries - subqueries

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

类似于:

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

"title": "Test Product 1",
"categories": [
  {
    "id": 1,
    "name": "category-1",
    "title": "Category 1",
    "level": 0
  },
  {
    "id": 2,
    "name": "category-2",
    "title": "Category 2",
    "level": 1
  }
]

我试过:

    $matchQuery = new Query\Match();
    $matchQuery->setField('categories.name', 'category-1');
    //I don't know how to add AND categories.level = 0

    $boolQuery1 = new Bool();
    $boolQuery1->addMust($matchQuery);

    $results = $record->search($boolQuery1)->getResults();

刚刚将您的布尔子句翻译成 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"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}