和或弹性搜索的组合 [英] Combination of and or elasticsearch
问题描述
如何在Elasticsearch中编写以下条件的查询
How to write query for following condition in elasticsearch
Select * from table1 where (cnd1 or cond2) and (cnd3)
我的cond2值来自嵌套对象.我的json对象在
My cond2 value is from nested object . My json object is below
details={ "name"="name1",
"address":"{
"city":"city1"
}"
}
我需要从物体上方获取城市
I need to take city from above object
details.address.city
以上语法是否正确,如果不正确,如何获取第二个对象城市的价值.
Is above syntax is right , if not how to get value of second object city.
推荐答案
您可以使用Elasticsearch轻松创建条件查询.但是您的数据部分存在一些奇怪的情况.
You can easily create a conditional queries with Elasticsearch. But there is some weird situation of your data section.
details={ "name"="name1",
"address":"{
"city":"city1"
}"
}
Elasticsearh将数据另存为json对象,但是您应该将数据另存为json.在本节中,您尝试发送一个对象.让我们检查一下:
Elasticsearh save your data as a json object, but you should give your data as a json. In this section, there is an object, you try to sent. Let us examine:
有一个detail对象的name属性,它是一个字符串.还有一个地址属性,它也是一个字符串.如果要通过 details.address.city
到达此对象,则该对象必须包含城市属性.现在我们尝试修复:
There is a name attribute of detail object, it is a string. And also there is a address attribute, and it is a string too. It should be an object which has to include a city attribute if you want to reach this object via details.address.city
. Now we try to fix:
{
"id":...,
...
"details": {
"name": "name1",
"address": {
"city": "city1"
}
}
}
在这种情况下,我删除了detail对象的双引号.现在,您可以将json的city属性作为json对象到达.现在,我们创建一个查询以覆盖城市:
In this case, I remove double quotation marks of details object. Now, you can reach city attribute of json as a json object. Now, we create a query to reach cities:
{
"query": {
"bool": {
"must": {
"term": {
"your-json-attribute": "???"
}
},
"should": [
{
"term": {
"your-json-attribute": "???"
}
},
{
"term": {
"your-json-attribute": "???"
}
}
]
}
}
}
我使用 term
查询,但是还有很多其他查询类型.您可以在文档中检查它们.但是对于 And
和 Or
,您可以使用 bool
查询.检查 https://www.elastic.co/guide/zh-CN/elasticsearch/reference/2.0/query-dsl-bool-query.html
I use term
query but there is lots of another query types. You can check them on documentation. But for And
and Or
, you can use bool
query. Check https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-bool-query.html
这篇关于和或弹性搜索的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!