Abhi.G
Abhi.G

Reputation: 2221

double nested elasticsearch query does not work for term clause

I am trying to run ES query on double nested document. Term query inside the nested query does not seem to work. If i replace the below term query with terms query it works. I am posting both mapping and query for your reference. Am i doing anything wrong? I am using ES version 6.4. Basically i want to an AND condition on the "bio.mut.type" field and return the docs accordingly.

Mapping:

{
    "assoc": {
        "date_detection": true,
        "properties": {
            "bio": {
                "type": "nested",
                "properties": {
                                                "node": {
                        "type": "keyword",
                        "index": "true"
                    },
                    "mut": {
                        "type": "nested",
                        "properties": {
                            "biomarkerType": {
                                "type": "keyword"
                            },
                            "createdBy": {
                                "type": "keyword"
                            },
                            "creationDate": {
                                "type": "date"
                            },
                            "domain": {
                                "type": "keyword"
                            },
                            "id": {
                                "type": "long"
                            },
                            "node": {
                                "type": "keyword"
                            },
                            "status": {
                                "type": "keyword"
                            },
                            "type": {
                                "type": "keyword"
                            },
                            "updateDate": {
                                "type": "date"
                            },
                            "updatedBy": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        },
        "version": {
            "type": "keyword"

        },
                "domain": {
            "type": "keyword"

        }
    }
}

QUERY:

{
  "bool" : {
    "must" : [
      {
        "nested" : {
          "query" : {
            "terms" : {
              "bio.node" : [
                "X"
              ],
              "boost" : 1.0
            }
          },
          "path" : "bio",
          "ignore_unmapped" : false,
          "score_mode" : "none",
          "boost" : 1.0
        }
      },
      {
        "nested" : {
          "query" : {
            "nested" : {
              "query" : {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "bio.mut.type" : {
                          "value" : "M",
                          "boost" : 1.0
                        }
                      }
                    },
                    {
                      "term" : {
                        "bio.mut.type" : {
                          "value" : "F",
                          "boost" : 1.0
                        }
                      }
                    },
                    {
                      "term" : {
                        "bio.mut.type" : {
                          "value" : "C",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              "path" : "bio.mut",
              "ignore_unmapped" : false,
              "score_mode" : "none",
              "boost" : 1.0
            }
          },
          "path" : "bio",
          "ignore_unmapped" : false,
          "score_mode" : "none",
          "boost" : 1.0
        }
      },
      {
        "terms" : {
          "domain" : [
            "X"
          ],
          "boost" : 1.0
        }
      }
    ],
    "must_not" : [
      {
        "terms" : {
          "status" : [
            "Deleted"
          ],
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

Sample Document:

{
    "_index": "XXX",
    "_type": "XXX",
    "_id": "3",
    "_score": 1.0,
    "_source": {
        "bio": [{
            "mut": [{
                    "id": 3,
                    "type": "M"
                },
                {
                    "id": 4,
                    "type": "F"
                },
                {
                    "id": 5,
                    "type": "C"
                }
            ]
        }]
    }
}

Upvotes: 0

Views: 471

Answers (1)

Val
Val

Reputation: 217304

Ok, so you're trying to match separate nested documents, i.e. return the top-level document that contains different nested documents that all match the nested queries.

Replace the second top-level must element by the following one:

    {
      "nested": {
        "query": {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "term": {
                      "bio.mut.type": {
                        "value": "M",
                        "boost": 1
                      }
                    }
                  },
                  "path": "bio.mut",
                  "ignore_unmapped": false,
                  "score_mode": "none",
                  "boost": 1
                }
              },
              {
                "nested": {
                  "query": {
                    "term": {
                      "bio.mut.type": {
                        "value": "F",
                        "boost": 1
                      }
                    }
                  },
                  "path": "bio.mut",
                  "ignore_unmapped": false,
                  "score_mode": "none",
                  "boost": 1
                }
              },
              {
                "nested": {
                  "query": {
                    "term": {
                      "bio.mut.type": {
                        "value": "C",
                        "boost": 1
                      }
                    }
                  },
                  "path": "bio.mut",
                  "ignore_unmapped": false,
                  "score_mode": "none",
                  "boost": 1
                }
              }
            ]
          }
        },
        "path": "bio",
        "ignore_unmapped": false,
        "score_mode": "none",
        "boost": 1
      }
    }

Upvotes: 1

Related Questions