0
votes

I've documents like these - Doc1:

{
    "attr1": "attrVal1",
    "nestedAttr": [
        {
           "id": 1,
           "type": "Type1",
           "output": "PASS"
        },
        {
           "id": 2,
           "type": "Type2",
           "output": "FAIL"
        },
        {
           "id": 3,
           "type": "Type1",
           "output": "PASS"
        }
    ]

}

Doc2:

{
    "attr1": "attrVal1",
    "nestedAttr": [
        {
           "id": 1,
           "type": "Type2",
           "output": "PASS"
        },
        {
           "id": 2,
           "type": "Type1",
           "output": "FAIL"
        },
        {
           "id": 3,
           "type": "Type1",
           "output": "PASS"
        }
    ]

}

nestedAttr is of nested mapping. I want to search of all attr1s which have "type": "Type1" and "output": "PASS". I don't care about the output of Type2 but if any of the Type1 has FAIL in a document, it should be ignored. So in above example, Doc1 will be returned for the query and Doc2 will not be returned.

I tried queries with permutation and combination of must and must_not but all are just searching for at least one match among the nested objects.

1

1 Answers

0
votes

Assuming your mapping for the nested object/collection is propery set up, you can use the nested query

Prepare: Delete the Index

DELETE test_nested

Prepare: Set the Nested-Mapping

PUT test_nested
{
  "mappings": {
    "properties": {
      "attr1": {
        "type": "text"
      },
      "nestedAttr": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "integer"
          },
          "type": {
            "type": "keyword"
          },
          "output": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

Prepare: Create doc #1

POST test_nested/_doc/1
{
  "attr1": "attrVal1",
  "nestedAttr": [
    {
      "id": 1,
      "type": "Type1",
      "output": "PASS"
    },
    {
      "id": 2,
      "type": "Type2",
      "output": "FAIL"
    },
    {
      "id": 3,
      "type": "Type3",
      "output": "PASS"
    }
  ]
}

Prepare: Create doc #2

POST test_nested/_doc/2
{
  "attr1": "attrVal1",
  "nestedAttr": [
    {
      "id": 1,
      "type": "Type2",
      "output": "PASS"
    },
    {
      "id": 2,
      "type": "Type1",
      "output": "FAIL"
    },
    {
      "id": 3,
      "type": "Type3",
      "output": "FAIL"
    }
  ]
}

Search: (nestedAttr.type:Type1 AND nestedAttr.output:PASS)

POST /test_nested/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "nestedAttr",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "nestedAttr.type": "Type1"
                    }
                  },
                  {
                    "match": {
                      "nestedAttr.output": "PASS"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Result: count(nestedAttr.type:Type1 AND nestedAttr.output:PASS) = 1

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7227666,
    "hits" : [
      {
        "_index" : "test_nested",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.7227666,
        "_source" : {
          "attr1" : "attrVal1",
          "nestedAttr" : [
            {
              "id" : 1,
              "type" : "Type1",
              "output" : "PASS"
            },
            {
              "id" : 2,
              "type" : "Type2",
              "output" : "FAIL"
            },
            {
              "id" : 3,
              "type" : "Type3",
              "output" : "PASS"
            }
          ]
        }
      }
    ]
  }
}