【发布时间】:2019-06-10 14:38:39
【问题描述】:
我正在使用 elasticsearch (2.3) 并尝试创建搜索查询。对于 elasticsearch,我需要 JSON,但我从一个简单的 php 数组开始,然后将其编码为 JSON。
我在创建正确的数组格式时遇到问题。
这是我正在使用的代码的一小部分:
$params['body']['query']['filtered']['filter']['bool']['must']["term"]['pprCategories']= "category1";
$params['body']['query']['filtered']['filter']['bool']['must_not']['exists']['field'] = "field1";
使用 if 语句添加新行:
$params['body']['query']['filtered']['filter']['bool']['must']['term']['country_name']= "country1";
所以总代码是:
$params['body']['query']['filtered']['filter']['bool']['must']["term"]['pprCategories']= "category1";
$params['body']['query']['filtered']['filter']['bool']['must_not']['exists']['field'] = "field1";
$params['body']['query']['filtered']['filter']['bool']['must']['term']['country_name']= "country1";
#output
echo "<pre>";
print_r(json_encode($params, JSON_PRETTY_PRINT));
echo "</pre>";
这是我从我构建的查询中得到的实际结果:
{
"body": {
"query": {
"filtered": {
"filter": {
"bool": {
"must": {
"term": {
"pprCategories": "category1",
"country_name": "country1"
}
},
"must_not": {
"exists": {
"field": "field1"
}
}
}
}
}
}
}
}
但是,期望的结果如下,即bool/must 数组中有两个元素:
{
"body": {
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"pprCategories": "category1"
}
},
{
"term": {
"country_name": "country1"
}
}
],
"must_not": {
"exists": {
"field": "field1"
}
}
}
}
}
}
}
}
如您所见,bool/must 数组包含两个条件,而不仅仅是一个:pprCategories 和 country_name 是两个不同的“必须”条件。
感谢您帮助我!
【问题讨论】:
-
对我来说,
must两次没有任何意义.. 所以想要的结果是无效的 -
您的 JSON(在键
bool的值中)有 2 个同名键:must- json 对象不能拥有它。 -
@DavidWinder 好的,清楚。我有 2 个(或更多)值,elasticsearch 2.3 必须匹配(而不是应该匹配)。知道如何创建 JSON 请求吗?
-
@deceze 这个问题很清楚,不知道你为什么要关闭它。
-
我有 2 个(或更多)值用于 elasticsearch ... 不知道你在说什么。请打开一个新问题
标签: php arrays elasticsearch