0

I am trying to filter some documents on ElasticSearch 7.11.

My index has the following mapping:

{
  "properties": {
    "recArrNested": {
      "type": "nested",
      "properties": {
        "nBTxt": {
          "type": "keyword"
        },
        "nBInt": {
          "type": "long"
        }
      }
    },
    "recNested": {
      "type": "object",
      "properties": {
        "nAInt": {
          "type": "long"
        },
        "nATxt": {
          "type": "keyword"
        }
      }
    },
    "recId": {
      "type": "keyword"
    }
  }
}

I have records which looks like that:

{
  "recArrNested": [
    {
      "nBTxt": "juliette",
      "nBInt": 10
    },
    {
      "nBTxt": "alpha",
      "nBInt": 42
    },
    {
      "nBTxt": "kilo",
      "nBInt": 11
    }
  ],
  "recNested": {
    "nAInt": 1,
    "nATxt": "manual"
  },
  "recId": "1alpha"
}

My goal is to filter the records which have a recArrNested.nBTxt equals to its recNested.nAInt NATO corresponding phonetic alphabet (alpha -> 1, bravo -> 2, and so on).

I have generated the following query:

{
  "size": 5,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "1"
                        }
                      }
                    },
                    {
                      "term": {
                        "recArrNested.nBTxt": {
                          "value": "alpha"
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "2"
                        }
                      }
                    },
                    {
                      "term": {
                        "recArrNested.nBTxt": {
                          "value": "bravo"
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "recId": {
        "order": "desc"
      }
    }
  ],
  "track_scores": false
}

Sadly the above example document does not match. Do you have any suggestion to tackle that query properly?

3
  • I just ingested your example document, ran the query and the document matched. Are you using any special mappings? Commented Feb 27, 2021 at 2:16
  • In order for this to work you probably need to make recArrNested and recNested of type nested in your index mapping, otherwise it won't work. Commented Feb 27, 2021 at 5:42
  • I have forgot to create it, but it does not change (even with nested instead of object). Commented Feb 27, 2021 at 7:15

1 Answer 1

1

I don't see a reason for declaring recNested to be of the type nested -- it'll suffice to keep recArrNested actually nested because you're dealing with arrays of objects that would've otherwise been flattened.

In accordance with your current mapping you'll want to use nested queries whenever applicable:

{
  "size": 5,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "1"
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "recArrNested",        <--
                        "query": {
                          "term": {
                            "recArrNested.nBTxt": {
                              "value": "alpha"
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "2"
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "recArrNested",        <--
                        "query": {
                          "term": {
                            "recArrNested.nBTxt": {
                              "value": "bravo"
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "recId": {
        "order": "desc"
      }
    }
  ],
  "track_scores": false
}
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.