Elasticsearch lowercase filter search

后端 未结 2 1512
长发绾君心
长发绾君心 2021-02-13 10:13

I\'m trying to search my database and be able to use upper/lower case filter terms but I\'ve noticed while query\'s apply analyzers, I can\'t figure out how to appl

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 10:52

    This may be achieved by appending .keyword to your field to query against the keyword version of the field. Assuming language was defined in the mapping with type keyword.

    Note that now only the exact text would match: mandarin won't match and Italian would.

    Your query would end up like this:

    {
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "language.keyword": "mandarin" // Returns Empty
                                }
                            },
                            {
                                "term": {
                                    "language.keyword": "Italian" // Returns Italian.
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

    Combining the term values is also allowed:

    {
        "query": {
            "filtered": {
                "filter": {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "language.keyword":
                                         ["mandarin", "Italian"]
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

提交回复
热议问题