0

I am trying to query a nested item but I am not sure how to formulate the query especially using javascript bodybuilder. Below is the mapping

I am trying to get all the active items (isActive: true) that have the "anomaly" id. I am not sure how to go about querying ElasticSearch. In SQL I could just do something like a contains or having. I am using Node.js bodybuilder on the backend to query elasticsearch.

Linkx_lair
  • 569
  • 1
  • 9
  • 21
  • could you check the mapping ? I think there is a problem in mapping json. – saba safavi Oct 01 '21 at 06:40
  • @sabasafavi fixed the json mapping. Not sure how do you query the nested anomaly id and 'isActive' fields in ElasticSearch. want to return all the items in the index that have a particular anomaly id – Linkx_lair Oct 01 '21 at 13:29
  • anomalies must be a nested type, but in the mapping json it is not nested, ```"anomalies": { "type":"nested", "properties": {"affects": { "type": "text" },....``` – saba safavi Oct 01 '21 at 14:21

1 Answers1

0

according to mapping,isActive and id are related to "anomalies" object. try this query

{
    "query": {
        "nested": {
            "query": {
                "bool": {
                    "filter": [
                        {
                            "match": {
                                "anomalies.isActive": {
                                    "query": "true"
                                }
                            }
                        },
                        {
                            "match": {
                                "anomalies.id": {
                                    "query": "any_value"
                                }
                            }
                        }
                    ],
                    "adjust_pure_negative": true,
                    "boost": 1.0
                }
            },
            "path": "anomalies",
            "ignore_unmapped": false,
            "score_mode": "none",
            "boost": 1.0
        }
    }
}
}
saba safavi
  • 160
  • 7
  • 20