bkahler
bkahler

Reputation: 365

Elasticsearch Partial Fields With Inner Hits

Is it possible to use the partial_fields parameter when performing a nested filter/query ?

For example this query will return just the fields I am looking for from the hits:

{
  "query": {
      "filtered": {
          "query": {
              "ids": {
                  "type": "users",
                  "values": [
                      "111"
                  ]
              }
          },
          "filter": {
              "nested": {
                  "path": "entitlements",
                  "filter": {
                      "bool": {
                          "must": [
                              {
                                  "term": {
                                    "program": "program-a"
                                  }
                              }
                         ]
                      }
                  },
                  "inner_hits": {
                      "size": 999
                  }
              }
          }
      }
  },
  "partial_fields": {
      "partial": {
          "include": [
              "entitlements.accountNumber",
              "entitlements.name",
              "entitlments.numbers"
          ]
      }
  }
}

However the inner_hits section, no fields are returned.

   "inner_hits": {
           "entitlements": {
              "hits": {
                 "total": 1,
                 "max_score": 1,
                 "hits": [
                    {
                       "_index": "myGreatIndex",
                       "_type": "users",
                       "_id": "111",
                       "_nested": {
                          "field": "entitlements",
                          "offset": 0
                       },
                       "_score": 1,
                       "fields": {
                          "partial": [
                             {}
                          ]
                       }
                    }
                 ]
              }
           }
        }

Is there a way to apply this for inner_hits ?

Upvotes: 3

Views: 9480

Answers (1)

Val
Val

Reputation: 217474

Inside the inner_hits section, you may use source filtering instead. partial_fields will work for outer hits and _source for inner_hits.

So you can specify your inner_hits like this:

"inner_hits": {
    "size": 999,
    "_source": [
          "accountNumber",
          "name",
          "numbers"
    ]
}

Note, though, that within your inner_hits results, you'll still get an empty partial array, but the fields you need will show up within the _source section.

Also worth noting that if you specify your partial_fields with unqualified names (see below), then partial fields will show up in your inner_hits results, but not in the outer hits anymore. So you need to figure out which works best for you.

"partial_fields": {
   "partial": {
     "include": [
        "accountNumber",
        "name",
        "numbers"
     ]
   }
}

Upvotes: 10

Related Questions