我试图找出一个时间段之间的名称。
但是问题是当我尝试运行它时
首先-返回选定的名称值
秒-它返回该时间段之间的数据。
现在我想做的是,我想在一段时间内搜索名称。
为了进一步说明,假设我们选择一个名称“john”,然后选择一个日期2/08/2015到2015年8月27日,
因此它应返回该时间段之间的所有名称。
但是它返回的是john的名称,而不是在此期间搜索其他文档,在搜索日期时,还应该使用名称“John”!
如何解决这个问题呢 !
$name = $request->request->get('name');
$strat_date = $request->request->get('strat_date');
$end_date = $request->request->get('end_date');
$params = array(
'index' => "myIndex",
'type' => "myType",
'body' => array(
'query' => array(
'bool' => array(
'must' => array(
// empty should clause for starters
)
)
)
)
);
// add each constraint in turn depending on whether the param is specified
if (!empty($sourceFilter)) {
$params['body']['query']['bool']['must'] = array(
'query_string' => array(
'default_field' => 'name',
'query' => implode(" ", $name)
)
);
}
if (!empty($end_date)) {
$params['body']['query']['bool']['must']['range'] = array(
'datehistory' => array(
"from" => $strat_date,
"to" => $end_date
)
);
}
// special case if none is present, just match everything
if (count($params['body']['query']['bool']['must']) == 0) {
$params['body']['query'] = array(
'match_all' => array()
);
}
$docs = $client->search($params);
但是总会返回这样的错误----
{"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[agEfJ6ltSJmlec3gpnPg3g][myIndex][1]: SearchParseException[[myIndex][1]: from[-1],size[-1]: Parse Failure [Failed to parse name [{\"query\":{\"bool\":{\"must\":{\"query_string\":{\"default_field\":\"name\",\"query\":\"john.se\"},\"range\":{\"datehistory\":{\"from\":1438207200,\"to\":1440626400}}}}}}]]]; nested: QueryParsingException[[myIndex] No query registered for [datehistory]]; }]","status":400}
最佳答案
您混合使用bool/should
和bool/must
,因此您只需要将bool/should
更改为bool/must
即可。
$params = array(
'index' => "myIndex",
'type' => "myType",
'body' => array(
'query' => array(
'bool' => array(
'must' => array( <--- CHANGE
// empty should clause for starters
)
)
)
)
);
// add each constraint in turn depending on whether the param is specified
if (!empty($sourceFilter)) {
$params['body']['query']['bool']['must'][] = array( <--- CHANGE
'query_string' => array(
'default_field' => 'name',
'query' => implode(" ", $name)
)
);
}
if (!empty($end_date)) {
$params['body']['query']['bool']['must'][] = array( <--- CHANGE
'range' => array(
'datehistory' => array(
"from" => $strat_date,
"to" => $end_date
)
)
);
}
// special case if none is present, just match everything
if (count($params['body']['query']['bool']['must']) == 0) { <-- CHANGE
$params['body']['query'] = array(
'match_all' => array()
);
}
$docs = $client->search($params);
关于php - 使两个查询互相作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32246307/