2
votes

I'm trying to run a wildcard query on a nested type in ElasticSearch. I have records with the following structure:

{
  "field_1": "value_1",
  "nested_field_1": [
    {
      "field_type": "some_field_type",
      "field_value": "some_value"
    },
    {
      "field_type": "another_field_type",
      "field_value": "another_value"
    }
  ]
}

I want to be able to run wildcard query on the nested_field, either on field_value or on field_type.

I can query for an exact match with this syntax:

  "query": {
    "nested": {
      "path": "nested_field_1",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "nested_field_1.field_value": "another_value"
              }
            }
          ]
        }
      }
    }
  }
}

But replacing the match with wildcard doesn't yield any results. Any help would be welcome.

1
can you show your wildcard query - user156327

1 Answers

1
votes

So I just tried your example and it gives me the result and used elasticsearch official wildcard query doc.

Index Def

{
    "mappings": {
        "properties": {
            "field_1": {
                "type": "text"
            },
            "nested_field_1" :{
                "type" : "nested",
                "properties" : {
                    "field_type" :{
                        "type" : "text"
                    },
                    "field_value" :{
                        "type" : "integer" --> created as interfere field
                    }
                }
            }
        }
    }
}

Index doc

{
    "field_1": "value_1",
    "nested_field_1": [
        {
            "field_type": "some_field_type",
            "field_value": 20
        },
        {
            "field_type": "another_field_type",
            "field_value": 40
        }
    ]
}

Wildcard search query

{
    "query": {
        "nested": {
            "path": "nested_field_1",
            "query": {
                "bool": {
                    "must": [
                        {
                            "wildcard": { --> note 
                                "nested_field_1.field_type": {
                                    "value": "another_field_type"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

Search result

  "nested_field_1": [
                        {
                            "field_type": "some_field_type",
                            "field_value": 20
                        },
                        {
                            "field_type": "another_field_type",
                            "field_value": 40
                        }
                    ]
                }