tail
tail

Reputation: 105

why case sensitive doesn't work in elasticSearch

I want to using case sensitive in Elasticsearch query_string

query_string: {
      default_field : 'message',
      query: 'info',
    }

If I enter info, the output be displayed info as well as INFO.

How to use case sensitive in Elasticsearch query_string?

Upvotes: 1

Views: 1452

Answers (3)

Anna Dominic
Anna Dominic

Reputation: 1

You could try using field 'message.keyword' if your mapping has set 'message' as an analyzed field. It will result in a case sensitive search.

Upvotes: 0

Amit
Amit

Reputation: 32386

Query strings are not discouraged for search bar or normal full-text searches as mentioned in official ES doc. From the same link:

Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.

If you don’t need to support a query syntax, consider using the match query. If you need the features of query syntax, use the simple_query_string query, which is less strict.

I would suggest, using the match query as recommended above, Which is analyzed and provides case insensitive search on text fields. So in your example you can define mapping as below:

"mappings": {
        "properties": {
            "message": {
                "type": "text" --> note `text` type which uses `standard` analyzer
            }
        }
    }

Index sample data(Notice case sensitive docs)

{
    "message": "foo"
}
{
    "message": "Foo"
}
{
    "message": "FOO"
}

And then use the below query to query data:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "message": "foo" -->you can change it to `Foo` and it will still give all results.
                    }
                }
            ]
        }
    }
}

And it gives all the results as shown below:

"hits": [
            {
                "_index": "querystring",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.13353139,
                "_source": {
                    "message": "FOO"
                }
            },
            {
                "_index": "querystring",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.13353139,
                "_source": {
                    "message": "Foo"
                }
            },
            {
                "_index": "querystring",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.13353139,
                "_source": {
                    "message": "foo"
                }
            }
        ]

Upvotes: 1

AMA
AMA

Reputation: 427

It all about your template and what is your field type and if it analyzed. you can check below for more details:

https://discuss.elastic.co/t/is-elasticsearch-querying-on-a-field-value-case-sensitive/74005

Upvotes: 0

Related Questions