How to search for a special character in an array of values in elastic search?

I have the following mapping of my index

{
    "settings": {
        "analysis": {
            "analyzer": {
                "custom_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "asciifolding",
                        "classic"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "title": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                },
                "analyzer": "custom_analyzer"
            },
            "description": {
                "type": "text",
                "analyzer": "custom_analyzer"
            },
            "thumbnailUrl": {
                "type": "text"
            },
            "attributes": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "text",
                        "analyzer": "custom_analyzer"
                    },
                    "value": {
                        "type": "text",
                        "analyzer": "custom_analyzer"
                    }
                }
            }
        }
    }
}

Digamos que tenho salvo o seguinte:

{
 title: "Avião"
 description: "Coração"
 attribute.name: "Aviação"
attribute.value: "Reprodução"
}

Se buscar por: “aviao” ou “coracao” ou “aviacao” ou “reproducao” deve trazer o resultado, ignorando caracter especial e case sensivel, se buscar por “Avião” ou “Coração” ou “Aviação” ou “Reprodução” também deve dar match.

Preciso buscar nos campos title, description, e no array attributes pelo name e value. O title e description funcionam, mas o attribute.name e attribute.value não funcionam.

my query:

{
    "query": {
        "nested": {
            "path": "attributes",
            "query": {
                "bool": {
                    "must": [
                        {
                            "multi_match": {
                                "query": "Aplicação",
                                "fields": [
                                    "title^3",
                                    "description^2",
                                    "attributes.name^2",
                                    "attributes.value^2"
                                ],
                                "type": "phrase",
                                "analyzer": "custom_analyzer"
                            }
                        }
                    ]
                }
            }
        }
    }
}

You need to separate nested part from from non-nested:

PUT test
{
    "settings": {
        "analysis": {
            "analyzer": {
                "custom_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "asciifolding",
                        "classic"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "title": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    }
                },
                "analyzer": "custom_analyzer"
            },
            "description": {
                "type": "text",
                "analyzer": "custom_analyzer"
            },
            "thumbnailUrl": {
                "type": "text"
            },
            "attributes": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "text",
                        "analyzer": "custom_analyzer"
                    },
                    "value": {
                        "type": "text",
                        "analyzer": "custom_analyzer"
                    }
                }
            }
        }
    }
}

PUT test/_bulk?refresh
{"index":{}}
{"title":"Avião","description":"Coração","attributes.name":"Aviação","attributes.value":"Reprodução"}

POST test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "Coração",
            "fields": [
              "title^3",
              "description^2"
            ],
            "type": "phrase"
          }
        },
        {
          "nested": {
            "path": "attributes",
            "query": {
              "bool": {
                "must": [
                  {
                    "multi_match": {
                      "query": "Coração",
                      "fields": [
                        "attributes.name^2",
                        "attributes.value^2"
                      ],
                      "type": "phrase"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Leave a Comment