假设我有一种查询语言,可以将“自然”语言查询解析为弹性搜索查询。
假设我们有以下查询:
(type == "my-type" || device_id == "some-device-id") && id == "123456789"
如何在弹性搜索中实现这一目标?
我尝试过:
{
"bool": {
"filter": {
"term": {
"id": "123456789"
}
},
"should": [
{
"term": {
"type": "my-type"
}
},
{
"term": {
"device_id": "some-device-id"
}
}
]
}
}
但这不是我想要的。
我在做什么错了?
映射:
{
"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"}
}
}
}
}
答案 0 :(得分:1)
在elasticsearch中,您可以像在bool query中将&&
视为must
和||
一样,以便翻译此查询
(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"
}
}
}
]
}
}
}
希望有帮助。