[ "gte" => $temp."-01-6ren">
gpt4 book ai didi

php - 在Elasticsearch中一起使用 “terms”和 “range”过滤器

转载 作者:行者123 更新时间:2023-12-02 22:31:50 24 4
gpt4 key购买 nike

我到处搜索,但没有一个答案符合我的要求。
我的代码是这样的:

文件1

<?php
if($year != 0)
{
$temp = strval($year);
$term =[
"range" => [
"decidedon" => [
"gte" => $temp."-01-01",
"lte" => $temp."-12-31"
]
]
];
array_push($params['body']['query']['filtered']['filter']['bool']['must'], $term);
}

文件2
<?php
$dummy = explode(" ",$cor);
$params['body']['query']['filtered']['filter']['bool']['must'] = ['terms'=> ['court' => $dummy] ];

我将它们包含在索引页的不同实例中,如下所示
if(isset($_GET['cor'])){
$cor = $_GET['cor'];
if(strcasecmp($cor,"all")!=0){
include 'courtfilter.php';
}
}

if(isset($_GET['year'])){
$year = $_GET['year'];
if($year != 0){
include 'yearfilter.php';
}
}

当我运行查询时,它说条款和范围不能嵌套在一起。
有人可以告诉我一种更好的方法,而不必使我的程序复杂或不必为这两个编写单独的过滤器吗?

(我是初学者,如果我做错了或太琐碎了,请原谅我。)

最佳答案

你快到了。在第二个文件中,您需要像这样将terms插入到must中:

$params['body']['query']['filtered']['filter']['bool']['must'] = [['terms'=> ['court' => $dummy]  ]];
^ ^
| |
add this .....................and this

bool/must必须是一个数组,而您直接为其分配了一个对象/哈希。

更好的方法是在包含任何文件之前(即在 must之前)初始化 if(isset($_GET['cor']))子句。您首先需要像这样初始化 bool/must数组:
$params['body']['query']['filtered']['filter']['bool']['must'] = [];

然后,在两个包含的文件中,都可以像在文件 array_push()中一样简单地执行 yearfilter.php

关于php - 在Elasticsearch中一起使用 “terms”和 “range”过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35033818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com