0

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.

2
  • Is each element in the array different documents ? Commented May 2, 2015 at 11:02
  • 1
    I think it should work the way you did it. The only mistake I see is that you performed a GET instead of a POST when querying your data. Commented May 2, 2015 at 11:08

1 Answer 1

2

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
                                    }
                                }
                            }
                        }
                   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.