我正在尝试查询基于两个值的文档 - 名称(字符串)和百分比(数字)。 示例是 - 我想要那些包含" audience.location.countries.name" ="美国"和#34; audience.location.countries.percentage" > 60.所以我想要一个有一个对象的文件{"名称":"美国","百分比":" 65"} 在这里," audience.location.countries"是一个具有两个属性的对象数组 - {" name"," percentage"}。 这是一个示例文档:
"location": {
"countries": [
{
"name": "CA",
"percentage": 4
},
{
"name": "GB",
"percentage": 5
},
{
"name": "JP",
"percentage": 8
},
{
"name": "US",
"percentage": 60
}
]}
这是我尝试过的一个查询,但它会抛出一个错误:" [和]查询格式错误,查询名称后没有start_object"
GET opensponsorship/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"isPublic": true
}
},
{
"term": {
"isDeleted": false
}
},
{
"and":[
{
"range": {
"audience.location.countries.name": "US"
}
},
{
"term":{
"audience.location.countries.percentage": {"gt": 59}
}
}
]
}
]
}
},
"size": "60",
"from": 0,
"sort": [
{
"followers": {
"order": "desc"
}
}
]
}
我是弹性搜索新手,知识非常有限。有人可以帮忙吗?
答案 0 :(得分:2)
据我所知,查询是"破坏"几个帐户:
and
query(BTW,您使用的ES版本?)range
和term
字段上的name
和percentage
过滤器混合audience.location.countries
应该是" nested object" 以下是解决问题1& 2.然后我会解释问题3:
GET opensponsorship/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"isPublic": true
}
},
{
"term": {
"isDeleted": false
}
},
{
"term": {
"audience.location.countries.name": "US"
}
},
{
"range": {
"audience.location.countries.percentage": {
"gt": 59
}
}
}
]
}
},
"size": "60",
"from": 0,
"sort": [
{
"followers": {
"order": "desc"
}
}
]
}
关于问题3,我建议您阅读elasticsearch中的nested fields。
简而言之 - 如果audience.location.countries
不是嵌套对象,那么你将会出现误报"结果是由于Elastic"如何变平"对象。
要解决此问题,您需要1)在映射中使audience.location.countries
成为嵌套对象类型,并且2)以下列方式使用嵌套查询包装contries
术语过滤器:
GET opensponsorship/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"isPublic": true
}
},
{
"term": {
"isDeleted": false
}
},
{
"nested": {
"path": "audience.location.countries",
"query": {
"bool": {
"filter": [
{
"term": {
"audience.location.countries.name": "US"
}
},
{
"range": {
"audience.location.countries.percentage": {
"gt": 59
}
}
}
]
}
}
}
}
]
}
},
"size": "60",
"from": 0,
"sort": [
{
"followers": {
"order": "desc"
}
}
]
}
希望这会有所帮助。祝你好运!