How to search in array of json elasticsearch-PHP


(Jorge Roberto) #1

Have this example data:

{
"id" : "129613",
"title" : "my city",
"coveragearea_without_cities" : [ {
"id" : 58,
"advertisement_id" : 129613,
"state_id" : 11,
"city_id" : 1630,
"created_at" : "2016-03-16 21:26:40",
"updated_at" : "2016-03-16 21:26:40",
"city" : {
"id" : 1630,
"name" : "Belo Horizonte",
"state_id" : 11,
"created_at" : "2016-03-15 19:37:53",
"updated_at" : "2016-03-15 19:37:53"
},
"state" : {
"id" : 11,
"name" : "Minas Gerais",
"acronym" : "MG",
"created_at" : "2016-03-15 19:37:53",
"updated_at" : "2016-03-15 19:37:53"
}
}, {
"id" : 59,
"advertisement_id" : 129613,
"state_id" : 11,
"city_id" : 1983,
"created_at" : "2016-03-16 21:26:40",
"updated_at" : "2016-03-16 21:26:40",
"city" : {
"id" : 1983,
"name" : "Juiz de Fora",
"state_id" : 11,
"created_at" : "2016-03-15 19:37:53",
"updated_at" : "2016-03-15 19:37:53"
},
"state" : {
"id" : 11,
"name" : "Minas Gerais",
"acronym" : "MG",
"created_at" : "2016-03-15 19:37:53",
"updated_at" : "2016-03-15 19:37:53"
}
}, {
"id" : 60,
"advertisement_id" : 129613,
"state_id" : 20,
"city_id" : null,
"created_at" : "2016-03-16 21:26:40",
"updated_at" : "2016-03-16 21:26:40",
"city" : null,
"state" : {
"id" : 20,
"name" : "Rio Grande do Norte",
"acronym" : "RN",
"created_at" : "2016-03-15 19:37:53",
"updated_at" : "2016-03-15 19:37:53"
}
} ]
}

How can do search in array json using elasticsearch-PHP ?


(Magnus B├Ąck) #2

Searching in nested fields and arrays isn't different from searching in fields on the top level, is it? What have you tried so far?


(Jorge Roberto) #3

i'm tried this search:

{
"index":"advertisements",
"type":"anounces",
"body":{
"sort":[{"premium":{"order":"desc"}}],
"query":{
"filtered":{
"filter":{
"bool":{
"should":{
"match":{"coveragearea_without_cities.state.name":"Mimoso do Sul"}
}
}
}
}
}
}
}


(system) #4