如何将 ElasticSearch 查询参数(DSL 查询)用于多种类型?
过去几个月我一直在使用 ElasticSearch,但当我必须传递复杂的查询时,仍然发现它很复杂。
我想运行必须搜索多个“类型”的查询,并且必须使用自己的“过滤器”搜索每种类型,但需要组合“搜索结果”
例如:
我需要搜索“用户类型” ”文档是我的朋友,同时我必须根据提供的关键字搜索我喜欢的“对象类型”文档。
OR
同时具有“AND”和“NOT”子句的查询
示例查询:
$options['query'] = array(
'query' => array(
'filtered' => array(
'query' => array(
'query_string' => array(
'default_field' => 'name',
'query' => $this->search_term . '*',
),
),
'filter' => array(
'and' => array(
array(
'term' => array(
'access_id' => 2,
),
),
),
'not' => array(
array(
'term' => array(
'follower' => 32,
),
),
array(
'term' => array(
'fan' => 36,
),
),
),
),
),
),
);
因为此查询旨在搜索 access_id = 2 的用户,但不得具有 id 32 的关注者和 id 36 的粉丝,
但这不是工作..
编辑:修改后的查询
{
"query": {
"filtered": {
"filter": {
"and": [
{
"not": {
"filter": {
"and": [
{
"query": {
"query_string": {
"default_field": "fan",
"query": "*510*"
}
}
},
{
"query": {
"query_string": {
"default_field": "follower",
"query": "*510*"
}
}
}
]
}
}
},
{
"term": {
"access_id": 2
}
}
]
},
"query": {
"field": {
"name": "xyz*"
}
}
}
}
}
现在运行此查询后,我得到两个结果,一个带有关注者:“34,518” &风扇:“510”,第二个风扇:“34”,但它不应该只是结果中的第二个。
有什么想法吗?
I have been working with the ElasticSearch from last few months, but still find it complicated when I have to pass an complicated query.
I want to run the query which will have to search the multiple "types" and each type has to be searched with its own "filters", but need to have combined "searched results"
For example:
I need to search the "user type" document which are my friends and on the same time i have to search the "object type" document which I like, according to the keyword provided.
OR
The query that has both the "AND" and "NOT" clause
Example query:
$options['query'] = array(
'query' => array(
'filtered' => array(
'query' => array(
'query_string' => array(
'default_field' => 'name',
'query' => $this->search_term . '*',
),
),
'filter' => array(
'and' => array(
array(
'term' => array(
'access_id' => 2,
),
),
),
'not' => array(
array(
'term' => array(
'follower' => 32,
),
),
array(
'term' => array(
'fan' => 36,
),
),
),
),
),
),
);
as this query is meant to search the user with access_id = 2, but must not have the follower of id 32 and fan of id 36
but this is not working..
Edit: Modified query
{
"query": {
"filtered": {
"filter": {
"and": [
{
"not": {
"filter": {
"and": [
{
"query": {
"query_string": {
"default_field": "fan",
"query": "*510*"
}
}
},
{
"query": {
"query_string": {
"default_field": "follower",
"query": "*510*"
}
}
}
]
}
}
},
{
"term": {
"access_id": 2
}
}
]
},
"query": {
"field": {
"name": "xyz*"
}
}
}
}
}
now after running this query, i am getting two results, one with follower: "34,518" & fan: "510" and second with fan:"34", but isn't it supposed to be only the second one in the result.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想查看我本月提供的演示文稿的幻灯片,其中解释了查询 DSL 工作原理的基础知识:
亲密关系 - ElasticSearch 查询 DSL 解释
您的查询的问题在于您的过滤器嵌套不正确。
and
和not
过滤器处于同一级别,但not
过滤器应位于and
下:You may want to look at the slides of a presentation that I gave this month, which explains the basics of how the query DSL works:
Terms of endearment - the ElasticSearch Query DSL explained
The problem with your query is that your filters are nested incorrectly. The
and
andnot
filters are at the same level, but thenot
filter should be underand
:我刚刚用“BOOL”尝试了
它给出了正确的答案。
但我不确定它应该这样使用吗?
I just tried it with the "BOOL"
It gives the correct answer.
but I'm not sure should it be used like this ?