我目前正在尝试将基于solr的应用程序迁移到elasticsearch.
我有这个lucene查询
((
name:(+foo +bar)
OR info:(+foo +bar)
)) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)
Run Code Online (Sandbox Code Playgroud)
据我所知,这是MUST子句与布尔OR组合的组合:
"获取包含(foo AND bar in name)OR(foo AND bar in info)的所有文档.之后过滤条件状态= 1,并提升具有图像的文档."
我一直试图使用一个bool查询,但我没有得到boolean OR到must子句.这是我有的:
GET /test/object/_search
{
"from": 0,
"size": 20,
"sort": {
"_score": "desc"
},
"query": {
"bool": {
"must": [
{
"match": {
"name": "foo"
}
},
{
"match": {
"name": "bar"
}
}
],
"must_not": [],
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,缺少"信息"的必须条件.
有没有人有办法解决吗?
非常感谢.
**更新**
我更新了我的弹性搜索查询并摆脱了该功能评分.我的基本问题仍然存在.
Dan*_*ell 339
例:
您想要查看所有项目(圆形AND(红色或蓝色)):
{
"query": {
"bool": {
"must": [
{
"term": {"shape": "round"}
},
{
"bool": {
"should": [
{"term": {"color": "red"}},
{"term": {"color": "blue"}}
]
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以执行更复杂的OR版本,例如,如果要匹配至少3个中的3个,则可以在"should"下指定5个选项并将"minimum_should"设置为3.
感谢Glen Thompson和Sebastialonso找到我的筑巢之前不太合适的地方.
还要感谢Fatmajk指出"术语"在ElasticSearch 6中变得"匹配".
Jes*_*sse 60
我终于设法创建了一个完全符合我想要的查询:
过滤的嵌套布尔查询.我不确定为什么没有记录.也许有人可以告诉我?
这是查询:
GET /test/object/_search
{
"from": 0,
"size": 20,
"sort": {
"_score": "desc"
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"state": 1
}
}
]
}
},
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"name": "foo"
}
},
{
"match": {
"name": "bar"
}
}
],
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
}
},
{
"bool": {
"must": [
{
"match": {
"info": "foo"
}
},
{
"match": {
"info": "bar"
}
}
],
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在伪SQL中:
SELECT * FROM /test/object
WHERE
((name=foo AND name=bar) OR (info=foo AND info=bar))
AND state=1
Run Code Online (Sandbox Code Playgroud)
请记住,这取决于您的文档字段分析和映射如何在内部处理name = foo.这可以从模糊到严格的行为变化.
"minimum_should_match":1表示,至少有一个should语句必须为true.
这个语句意味着只要结果集中有一个包含has_image:1的文档,它就会被因子100提升.这会改变结果排序.
"should": [
{
"match": {
"has_image": {
"query": 1,
"boost": 100
}
}
}
]
Run Code Online (Sandbox Code Playgroud)
有乐趣的人:)
我最近也不得不解决这个问题,经过大量的反复试验,我想出了这个(在 PHP 中,但直接映射到 DSL):
'query' => [
'bool' => [
'should' => [
['prefix' => ['name_first' => $query]],
['prefix' => ['name_last' => $query]],
['prefix' => ['phone' => $query]],
['prefix' => ['email' => $query]],
[
'multi_match' => [
'query' => $query,
'type' => 'cross_fields',
'operator' => 'and',
'fields' => ['name_first', 'name_last']
]
]
],
'minimum_should_match' => 1,
'filter' => [
['term' => ['state' => 'active']],
['term' => ['company_id' => $companyId]]
]
]
]
Run Code Online (Sandbox Code Playgroud)
它映射到 SQL 中的类似内容:
SELECT * from <index>
WHERE (
name_first LIKE '<query>%' OR
name_last LIKE '<query>%' OR
phone LIKE '<query>%' OR
email LIKE '<query>%'
)
AND state = 'active'
AND company_id = <query>
Run Code Online (Sandbox Code Playgroud)
这一切的关键是minimum_should_match设置。没有这个,将filter完全覆盖should.
希望这可以帮助某人!
这样,您可以使用Kibana在多个外部布尔查询中嵌套多个布尔查询,
布尔值表示我们正在使用布尔值
必须是AND
应该是或
GET my_inedx/my_type/_search
{
"query" : {
"bool": { //bool indicates we are using boolean operator
"must" : [ //must is for **AND**
{
"match" : {
"description" : "some text"
}
},
{
"match" :{
"type" : "some Type"
}
},
{
"bool" : { //here its a nested boolean query
"should" : [ //should is for **OR**
{
"match" : {
//ur query
}
},
{
"match" : {}
}
]
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是在ES中嵌套查询的方式
“布尔”中有更多类型,例如-
过滤
一定不
小智 5
如果您使用的是 Solr 的默认或 Lucene 查询解析器,您几乎总是可以将其放入查询字符串查询中:
POST test/_search
{
"query": {
"query_string": {
"query": "(( name:(+foo +bar) OR info:(+foo +bar) )) AND state:(1) AND (has_image:(0) OR has_image:(1)^100)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,您可能想要使用布尔查询,例如您已经发布的查询,或者甚至是两者的组合。
| 归档时间: |
|
| 查看次数: |
105375 次 |
| 最近记录: |