Pat*_*ryk 1 elasticsearch elasticsearch-6 term-query
假设我有一种查询语言,可以将“自然”语言查询解析为弹性搜索查询。
假设我们有以下查询:
(type == "my-type" || device_id == "some-device-id") && id == "123456789"
Run Code Online (Sandbox Code Playgroud)
我怎样才能在弹性搜索中实现这一点?
我尝试过:
{
"bool": {
"filter": {
"term": {
"id": "123456789"
}
},
"should": [
{
"term": {
"type": "my-type"
}
},
{
"term": {
"device_id": "some-device-id"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的。
我究竟做错了什么?
映射:
{
"mappings": {
"_doc": {
"properties": {
"accepted_at": {
"type": "date",
"format": "strict_date_optional_time||date_time||epoch_millis"
},
"created_at": {
"type": "date",
"format": "strict_date_optional_time||date_time||epoch_millis"
},
"device_id": {"type": "keyword"},
"id": {"type": "keyword"},
"location": {"type": "geo_point"},
"message_id": {"type": "keyword"},
"type": {"type": "keyword"}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在elasticsearch中你可以认为bool查询&&中的asmust和||as should ,所以你可以翻译这个查询
(type == "my-type" || device_id == "some-device-id") && id == "123456789"
作为
must(should(type == "my-type"),(device_id == "some-device-id"), id == "123456789")
当我们想要将其转换为 elasticsearch DSL 时,它给了我们这个查询
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"term": {
"type": {
"value": "my-type"
}
}
},
{
"term": {
"device_id": {
"value": "some-device-id"
}
}
}
]
}
},
{
"term": {
"id": {
"value": "123456789"
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望有帮助。
| 归档时间: |
|
| 查看次数: |
1112 次 |
| 最近记录: |