user4489520
user4489520

Reputation:

How to filter Inner Objects in Elasticsearch?

I have a contacts field in my documents in Elasticsearch. each element inside the contacts field is an Object itself. I want to use term or terms filter on contacts field so that it matches documents where contacts.province_id is X. I have tried contacts.province_id as search field but it doesn't work. How should I filter these kinds of fields?

"contacts": 
[
  {
     "id": 1,
     "address": "address1",
     "tel": "40 07 13 22",
     "doctor_id": 1,
     "type_id": 1,
     "lng": "51.374720",
     "lat": "35.781986",
     "city_id": 186,
     "province_id": 8,
     "hour_about": null,
     "place_name": null
  },
  {
     "id": 2,
     "address": "address2",
     "tel": null,
     "doctor_id": 1,
     "type_id": 2,
     "lng": "51.520313",
     "lat": "35.726983",
     "city_id": 186,
     "province_id": 8,
     "hour_about": null,
     "place_name": null
  },
  {
     "id": 3,
     "address": "address3",
     "tel": null,
     "doctor_id": 1,
     "type_id": 2,
     "lng": "51.456368",
     "lat": "35.797505",
     "city_id": 186,
     "province_id": 8,
     "hour_about": null,
     "place_name": null
  }
]

EDIT :

I've tried this Query :

GET /index_name/type_name/_search
{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "contacts.province_id": 8
                }
            }
        }
    }
}

but it returns 3 results, I expect 5 results. whats the problem?

Thanks for your help.

Upvotes: 0

Views: 2157

Answers (1)

Prabin Meitei
Prabin Meitei

Reputation: 2000

What is the mapping you have used? If you are using nested type then try using the nested filter.

    {
         "filter": {
                "nested": {
                                "path": "contacts",
                                "filter": {
                                    "term": {
                                        "contacts.province_id": 3
                                    }
                                }
                            }
                        }
                   }

Upvotes: 2

Related Questions