无法使用Elastica构建查询

时间:2019-07-23 13:32:48

标签: elasticsearch kibana elastica

我想使用Elastica(elasticsearch PHP客户端)生成Elasticsearch查询,我将首先通过Kibana手动构建和测试查询,然后在PHP中实现它。

在我的数据库中有学术机构,我想寻找类型= '12 -SUP'的机构;这些学位的至少(法语):PBAC-BAC2-CDV,PBAC-BAC3-CDV

我正在使用Kibana构建此过滤器: enter image description here

这是第一个过滤器的查询:

{
   "query": {
     "match": {
       "type": {
         "query": "12-SUP",
         "type": "phrase"
       }
     }
   }
}

enter image description here

第二个:

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "formations": "PBAC-BAC2-CDV"
          }
        },
        {
          "match_phrase": {
            "formations": "PBAC-BAC3-CDV"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }

enter image description here

结果还不错。

然后我合并两个查询,并在Dev Tools中对其进行午餐:

GET aep_etablissement/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "type": [
                            "12-SUP"
                        ]
                    }
                }

            ],

            "minimum_should_match": 1,


            "should": [{
                    "match_phrase": {
                        "formations": {
                            "query": "PBAC-BAC2-CDV"
                        }
                    }
                },
                {
                    "match_phrase": {
                        "formations": {
                            "query": "PBAC-BAC3-CDV"
                        }
                    }
                }
            ]
        }
    }
}

enter image description here

太好了:结果还不错:)

现在,我想使用Elastica生成相同的最终查询,这是我的代码:

private function filtreType(array $incluses): void
{
    $terms = new Query\Terms();
    $terms->setTerms('type', $incluses);
    $this->filtre->addMust($terms);
} 
...
private function formations(array $formations): void
{
    foreach ($formations as $forma) {
        $query = new MultiMatch();
        $query->setMinimumShouldMatch('1');
        $query->setQuery($forma);
        $query->setFields(['formations']);
        $this->filtre->addShould($query);
    }
}

这是生成的查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "type": [
                            "12-SUP"
                        ]
                    }
                }
            ],
            "should": [
                {
                    "multi_match": {
                        "minimum_should_match": "1",
                        "query": "PBAC-BAC2-CDV",
                        "fields": [
                            "formations"
                        ]
                    }
                },
                {
                    "multi_match": {
                        "minimum_should_match": "1",
                        "query": "PBAC-BAC3-CDV",
                        "fields": [
                            "formations"
                        ]
                    }
                }
            ]
        }
    }
}

如您所见,查询不一样,最后查询的结果为false!我不知道如何使用Elastica编写此文件,请帮忙:(

这里是一个差异,因此您可以清楚地看到:https://www.diffchecker.com/vPU0e8xS

此致:)

0 个答案:

没有答案