Sush
Sush

Reputation: 317

To find exact result of match from elastic query

I am struggling with searching for elastic data.

please check the below data,

"OwnerList": [
                        {
                            "contacttp": "Asset Manager",
                            "artrole": null,
                            "ownerflag": "N",
                            "userid": "sumshett - (Somshekhar Umshetti)",
                            "cecid": "SUMSHETT"
                        },
                        {
                            "contacttp": "Deal Assurance POC",
                            "artrole": null,
                            "ownerflag": "N",
                            "userid": "ngadi - (Neelima Gadi)",
                            "cecid": "NGADI"
                        },
                        {
                            "contacttp": "True-Up Specialist",
                            "artrole": null,
                            "ownerflag": "Y",
                            "userid": "sumshett - (Somshekhar Umshetti)",
                            "cecid": "SUMSHETT"
                        },
                        {
                            "contacttp": "Renewals Manager",
                            "artrole": "CSS ACAT Admin",
                            "ownerflag": "Y",
                            "userid": "richgup3 - (Richa Gupta)",
                            "cecid": "richgup3"
                        },
                        {
                            "contacttp": "True-Up Specialist-Backup",
                            "artrole": "CSS ACAT Admin",
                            "ownerflag": "Y",
                            "userid": "richgup3 - (Richa Gupta)",
                            "cecid": "richgup3"
                        },
                        {
                            "contacttp": "True-Up Specialist-Backup",
                            "artrole": "CSS ACAT Account Maintenance",
                            "ownerflag": "N",
                            "userid": "ngadi - (Neelima Gadi)",
                            "cecid": "ngadi"
                        },
                        {
                            "contacttp": "True-Up Specialist-Backup",
                            "artrole": "CSS ACAT Admin",
                            "ownerflag": "Y",
                            "userid": "mahengup - (Mahendra Gupta)",
                            "cecid": "mahengup"
                        }
                    ]

I just want to check that data is found in the above array or not. for that, I working with the below query, but it not giving the correct output.

I want query that match with same object value like OwnerList.ownerflag and cecid from same object. but below query matching with any data from array.

{
  "size": 1000,
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "OwnerList.ownerflag": "Y"
          }
        },
        {
          "match": {
            "OwnerList.cecid": "ngadi"
          }
        }
      ]
    }
  }
}

Thanks in advance.

Upvotes: 0

Views: 52

Answers (1)

Bhavya
Bhavya

Reputation: 16192

You need to use nested data type

Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type.

Adding a working example

Index mapping:

{
  "mappings": {
    "properties": {
      "OwnerList": {
        "type": "nested"
      }
    }
  }
}

If you want to match with the same object value like OwnerList.ownerflag and OwnerList.cecid from the same object, use the below query

Search Query:

{
  "query": {
    "nested": {
      "path": "OwnerList",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "OwnerList.ownerflag": "Y"
              }
            },
            {
              "match": {
                "OwnerList.cecid": "ngadi"
              }
            }
          ]
        }
      },
      "inner_hits":{}
    }
  }
}

Since in the index data given above in the question, there is no data that contains "OwnerList.ownerflag": "Y" AND "OwnerList.cecid": "ngadi", therefore the document will not match

Upvotes: 1

Related Questions