【问题标题】:Elasticsearch array format for multiple must terms多个必须项的 Elasticsearch 数组格式
【发布时间】: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


【解决方案1】:

如果你想添加几个条件,你需要明白bool/must应该是一个数组。你可以让你的代码像这样工作:

// first create the bool/must array with a single term condition
$params['body']['query']['filtered']['filter']['bool']['must'] = [ 
   [ 
     'term' => [
        'pprCategories' => "category1"; 
     ]
   ] 
];
// add the must_not condition
$params['body']['query']['filtered']['filter']['bool']['must_not']['exists']['field'] = "field1";

// test your condition
if (...) {
    // now add a second constraint to the bool/must array
    $params['body']['query']['filtered']['filter']['bool']['must'][] = [ 
        'term' => [
            'country_name' => "country1" 
        ]
    ];
}

【讨论】:

  • @5dale5 运气好吗?
  • 在这种情况下,输出将是: ` "must": [ { "term": { "pprCategories": "category1" } }, { "term": { "country_name": " country1" } } ], "must_not": { "exists": { "exists": { "field": "field1" }`。所以一个数组在学期......不工作
  • 这是在不同字段上添加多个必须约束的唯一方法。请尝试查询,并报告。另请注意,您有两次exists,因此您构建查询的方式肯定有其他问题
  • 是的,只是为了测试它。有用! Elasticsearch 以这种方式接受它。其他解决方案类似于@LuisLeonGonzalez,并且可以在 bij 添加行,例如$params1['body']['query']['filtered']['filter']['bool']['must'][]["term"]['pprCategories']= "category1"; 问题解决了!谢谢大家!
  • 酷,很高兴我们知道了!
【解决方案2】:

我认为你应该先这样做,然后再将其转换为 json。

$params['body']['query']['filtered']['filter']['bool']['must'][0]["term"]['pprCategories']= "category1";
$params['body']['query']['filtered']['filter']['bool']['must'][1]["term"]['country_name']= "country1";
$params['body']['query']['filtered']['filter']['bool']['must'][2]["term"]['population_name']= "population1";
$params['body']['query']['filtered']['filter']['bool']['must'][3]["term"]['kilometers_amount']= "55145";

【讨论】:

  • 数组必须在must级别而不是term一级
猜你喜欢
  • 2014-02-05
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
相关资源
最近更新 更多