1

Having a problem getting record with intersecting ('and') condition. I have a doc:

{
        "uuid": "1e2a0c06-af24-42e1-a31a-0f84233521de",
        "subject": "subj",
        "relations": [
            {
                "userUuid": "0f38e576-6b1f-4c1a-86a8-67a55a06d504",
                "signed": false
            },
            {
                "userUuid": "15979293-6b04-41a9-a6aa-bba99499496f",
                "signed": true
            }
        ]
}

Querying and expecting to get EMPTY result, cause conditions are met from different nested elements:

    "bool": {
        "must": [
            {
                "nested": {
                    "query": {
                        "term": {
                            "relations.userUuid": {
                                "value": "15979293-6b04-41a9-a6aa-bba99499496f",
                                "boost": 1.0
                            }
                        }
                    },
                    "path": "relations",
                    "ignore_unmapped": false,
                    "score_mode": "none",
                    "boost": 1.0
                }
            },
            {
                "nested": {
                    "query": {
                        "term": {
                            "relations.signed": {
                                "value": false,
                                "boost": 1.0
                            }
                        }
                    },
                    "path": "relations",
                    "ignore_unmapped": false,
                    "score_mode": "none",
                    "boost": 1.0
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1.0
    }
}

How to query that condition would be 'AND' within same nested object?

1 Answer 1

1

Updated the answer looking at your comment. You need to mention path in your nested document.

Scenario 1: If you want any of the nested documents to contain 5979293-6b04-41a9-a6aa-bba99499496f as userUuid and signed as true

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "relations",         <---- Note this
            "query": {
              "term": {
                "relations.userUuid": "15979293-6b04-41a9-a6aa-bba99499496f"
              }
            }
          }
        },
        {
          "nested": {
            "path": "relations",
            "query": {
              "term": {
                "relations.signed": false
              }
            }
          }
        }
      ]
    }
  }
}

This would return true if there are two nested documents, first nested doc containing the userUuid and second nested doc containing signed as false

Scenario 2: If you want both the fields to be present in a single nested document

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "relations",       <---- Note this
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "relations.userUuid": "15979293-6b04-41a9-a6aa-bba99499496f"
                    }
                  },
                  {
                    "term": {
                      "relations.signed": false
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

In this scenario, a single nested document must contain both values.

Let me know if this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

No, mapping is: ' _mappings?pretty" -H 'Content-Type: application/json' -d' { "dynamic": false, "properties" : {"relations": {"type": "nested","properties": { "userUuid": { "type": "keyword" }, .......'
anyway, I tried to add '.keyword', this hasn't helped. I mean now it don't find even matched conditions.
Could you please check the answer now. I've updated it accordingly.
What you need is the second query with the value of relations.signed as false as you want to return only those documents which contain both values in a single nested document

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.