Hi all.
I'm using ElasticSearch 2.4
I need to create a search query between main object and nested object, if I use the AND condition it works correctly, but the problem is if I try to use OR conditional between main object and nested object:
Please review the code below and tell me if there is a way to make it work using OR conditional.
Create mapping:
PUT /example_contact_purchases
{
"mappings": {
"contact": {
"dynamic": false,
"properties": {
"name": {
"type": "string"
},
"country": {
"type": "string"
},
"purchases": {
"type": "nested",
"properties": {
"uuid":{
"type":"string"
}
}
}
}
}
}
}
Mapping result:
GET example_contact_purchases/_mapping
{
"example_contact_purchases": {
"mappings": {
"contact": {
"dynamic": "false",
"properties": {
"country": {
"type": "string"
},
"name": {
"type": "string"
},
"purchases": {
"type": "nested",
"properties": {
"uuid": {
"type": "string"
}
}
}
}
}
}
}
}
Create First Item:
POST example_contact_purchases/contact
{
"name" : "Fran",
"country": "ES",
"purchases" : [
{
"uuid" : "23"
}
]
}
Create Second Item:
POST example_contact_purchases/contact
{
"name" : "Jhon",
"country": "UK",
"purchases" : [
{
"uuid" : "45"
}
]
}
Create Third Item:
POST example_contact_purchases/contact
{
"name" : "Leonardo",
"country": "IT",
"purchases" : [
{
"uuid" : "45"
}
]
}
Example Query: Country == ES AND purchase.uuid == 23
GET example_contact_purchases/_search
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":"country:ES"
}
}
}
},
"filter":{
"nested":{
"path":"purchases",
"filter":{
"query":{
"query_string":{
"query":"(purchases.uuid:23)"
}
}
}
}
}
}
Result:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "example_contact_purchases",
"_type": "contact",
"_id": "AW_nkURti9zva2kl7ESR",
"_score": 1,
"_source": {
"name": "Fran",
"country": "ES",
"purchases": [
{
"uuid": "23"
}
]
}
}
]
}
}
Target Query: Country== "ES" OR purchase.uuid== 45