How do we query Elastic Search when there is a JSON object inside a nested type?

My mapping looks like:

{
  "mappings": {
    "dynamic": "strict",
    "_meta": {
      "version": 9
    },
    "properties": {
      "__myVersion": {
        "type": "integer"
      },
      "businessKey": {
        "type": "keyword"
      },
      "businessStatus": {
        "type": "keyword"
      },
      "candidateGroups": {
        "type": "keyword"
      },
      "definitionCategory": {
        "type": "keyword"
      },
      "definitionId": {
        "type": "keyword"
      },
      "definitionKey": {
        "type": "keyword"
      },
      "definitionName": {
        "type": "text",
        "copy_to": [
          "full_text_typeAhead"
        ]
      },
      "definitionVersion": {
        "type": "integer"
      },
      "deploymentId": {
        "type": "keyword"
      },
      "endTime": {
        "type": "date"
      },
      "fullTextVariables": {
        "type": "text",
        "analyzer": "ngrams",
        "search_analyzer": "standard"
      },
      "full_text_typeAhead": {
        "type": "text",
        "analyzer": "ngrams",
        "search_analyzer": "standard"
      },
      "id": {
        "type": "keyword"
      },
      "identityLinks": {
        "type": "nested",
        "properties": {
          "groupId": {
            "type": "keyword"
          },
          "id": {
            "type": "keyword"
          },
          "type": {
            "type": "keyword"
          },
          "userId": {
            "type": "keyword"
          }
        }
      },
      "involvedUsers": {
        "type": "keyword"
      },
      "name": {
        "type": "text",
        "copy_to": [
          "nameKeyword",
          "full_text_typeAhead"
        ]
      },
      "nameKeyword": {
        "type": "keyword"
      },
      "parentScopeBusinessKey": {
        "type": "keyword"
      },
      "parentScopeBusinessStatus": {
        "type": "keyword"
      },
      "parentScopeDefinitionCategory": {
        "type": "keyword"
      },
      "parentScopeDefinitionId": {
        "type": "keyword"
      },
      "parentScopeDefinitionKey": {
        "type": "keyword"
      },
      "parentScopeDefinitionName": {
        "type": "text"
      },
      "parentScopeId": {
        "type": "keyword"
      },
      "parentScopeName": {
        "type": "text"
      },
      "parentScopeType": {
        "type": "keyword"
      },
      "rootScopeBusinessKey": {
        "type": "keyword"
      },
      "rootScopeBusinessStatus": {
        "type": "keyword"
      },
      "rootScopeDefinitionCategory": {
        "type": "keyword"
      },
      "rootScopeDefinitionId": {
        "type": "keyword"
      },
      "rootScopeDefinitionKey": {
        "type": "keyword"
      },
      "rootScopeDefinitionName": {
        "type": "text"
      },
      "rootScopeId": {
        "type": "keyword"
      },
      "rootScopeName": {
        "type": "text"
      },
      "rootScopeType": {
        "type": "keyword"
      },
      "startTime": {
        "type": "date"
      },
      "startUserId": {
        "type": "keyword"
      },
      "state": {
        "type": "text"
      },
      "tenantId": {
        "type": "keyword"
      },
      "translations": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "keyword"
          },
          "key": {
            "type": "keyword"
          },
          "locale": {
            "type": "keyword"
          },
          "scopeHierarchyType": {
            "type": "keyword"
          },
          "value": {
            "type": "text"
          }
        }
      },
      "type": {
        "type": "keyword"
      },
      "variables": {
        "type": "nested",
        "properties": {
          "booleanValue": {
            "type": "boolean"
          },
          "dateValue": {
            "type": "date"
          },
          "decimalValue": {
            "type": "double"
          },
          "id": {
            "type": "keyword"
          },
          "jsonValue": {
            "type": "object",
            "dynamic": "true",
            "enabled": false
          },
          "name": {
            "type": "keyword"
          },
          "numberValue": {
            "type": "long"
          },
          "rawValue": {
            "type": "text",
            "index": false
          },
          "revision": {
            "type": "integer"
          },
          "scopeDefinitionId": {
            "type": "keyword"
          },
          "scopeDefinitionKey": {
            "type": "keyword"
          },
          "scopeHierarchyType": {
            "type": "keyword"
          },
          "scopeId": {
            "type": "keyword"
          },
          "scopeType": {
            "type": "keyword"
          },
          "textValue": {
            "type": "text"
          },
          "textValueKeyword": {
            "type": "keyword"
          },
          "type": {
            "type": "keyword"
          },
          "variableTypeAhead": {
            "type": "text",
            "copy_to": [
              "fullTextVariables"
            ]
          }
        }
      }
    }
  }
}

I am trying to find documents where the definitionKey is some value and a field called ExternalReference inside and object that is itself inside the jsonValue object in the mapping.
Partial doc looks like:

....
        "jsonValue": {
          "Id": "f6ae763b-b8eb-4824-bf94-f3f376283925",
          "Data": {
            "ExternalReference": "5495375.100122493",
            "dealNumber": "015"
          }
       }
....

I have tried a lot of combinations of nested queries, with the latest being (hoping the flattened objects will help) but no luck. Can someone share any ideas?

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "definitionKey": "CRED_C001"
          }
        },
        {
          "nested": {
            "path": "variables",
            "query": {
              "match": {
                "variables.jsonValue.Data.ExternalReference": "5495375.100122493"
              }
            }
          }
        }
      ]
    }
  }
}

>Solution :

Your query is correct, however, the problem is that jsonValue is configured with "enabled": false, which means that none of the fields/values inside jsonValue have been indexed, and thus, none of them are searchable.

This is not a setting that can be modified dynamically, you need to reindex your data into a new index in order for your search to work.

Leave a Reply