在弹性搜索 PHP 包中使用 "And" 运算符搜索
Search with "And" operator in Elastic search PHP package
我正在尝试在 php composer 包的帮助下学习 Elastic Search
。我有一个名为 media_data
的 index
,其中包含 nits_account
、nits_url
、session_id
、timestamp
字段。我想要基于上述字段的过滤器,它应该在 and
运算符中。我当前的查询代码是:
$items = $this->elasticsearch->search([
'index' => $index_name,
'body' => [
'query' => [
'filtered' => [
'filter' => [
'and' => [
['match' => ['nits_account' => 'xyzABCD2190-aldsj']], //API Key
['match' => ['nits_url' => 'google.com']],
]
]
]
]
]
]);
我的问题:
我无法获取数据。但是,如果我执行以下代码:
$items = $this->elasticsearch->search([
'index' => $index_name,
'body' => [
'query' => [
'bool' => [
'should' => [
['match' => ['nits_account' => $account] ],
['match' => ['nits_url' => $domain] ],
],
],
]
]
]);
我在 or
运算符中获取值,但需要在其中进行 and
运算。
- 如何对各个字段进行不同的搜索操作,我的意思是我希望
nits_account
字段完全匹配,我希望 nits_url
与 like/wildcard 操作,时间戳应该是可比较的(大于 than/less than/between 两个日期)。
试试这个:
$items = $this->elasticsearch->search([
'index' => $index_name,
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['nits_account' => $account] ],
['match' => ['nits_url' => $domain] ]
],
],
]
]
]);
您应该使用 must
关键字,而不是 should
关键字。 must
类似于 AND 操作,而 should
类似于 OR 操作。
看到这个
如果您需要使用条件匹配,请使用如下内容:
"query": {
"bool": {
"must": [
{
"range": {
"nits_url": {
"gte": 1000,
"lte": 10000
}
}
},
{
"match": {
"nits_account": "$account"
}
}
]
}
}
我正在尝试在 php composer 包的帮助下学习 Elastic Search
。我有一个名为 media_data
的 index
,其中包含 nits_account
、nits_url
、session_id
、timestamp
字段。我想要基于上述字段的过滤器,它应该在 and
运算符中。我当前的查询代码是:
$items = $this->elasticsearch->search([
'index' => $index_name,
'body' => [
'query' => [
'filtered' => [
'filter' => [
'and' => [
['match' => ['nits_account' => 'xyzABCD2190-aldsj']], //API Key
['match' => ['nits_url' => 'google.com']],
]
]
]
]
]
]);
我的问题:
我无法获取数据。但是,如果我执行以下代码:
$items = $this->elasticsearch->search([ 'index' => $index_name, 'body' => [ 'query' => [ 'bool' => [ 'should' => [ ['match' => ['nits_account' => $account] ], ['match' => ['nits_url' => $domain] ], ], ], ] ] ]);
我在 or
运算符中获取值,但需要在其中进行 and
运算。
- 如何对各个字段进行不同的搜索操作,我的意思是我希望
nits_account
字段完全匹配,我希望nits_url
与 like/wildcard 操作,时间戳应该是可比较的(大于 than/less than/between 两个日期)。
试试这个:
$items = $this->elasticsearch->search([
'index' => $index_name,
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['nits_account' => $account] ],
['match' => ['nits_url' => $domain] ]
],
],
]
]
]);
您应该使用 must
关键字,而不是 should
关键字。 must
类似于 AND 操作,而 should
类似于 OR 操作。
看到这个
如果您需要使用条件匹配,请使用如下内容:
"query": {
"bool": {
"must": [
{
"range": {
"nits_url": {
"gte": 1000,
"lte": 10000
}
}
},
{
"match": {
"nits_account": "$account"
}
}
]
}
}