字段中的大写字母导致查询在Elasticsearch中失败

问题描述

我有一个查询,用于搜索包含某些值的成绩单。它应该匹配属于特定租户的每个文档。当查询的术语过滤器没有大写字母时,该查询有效,但在查询过滤器失败时会失败。

我应该将租户ID编码为仅使用小写字母的格式吗?还是有办法使术语过滤器(或一些类似的过滤器)区分大小写?还是我需要在tenant_id字段上启用某些选项以保留其大小写?

{
    'query': {
        'bool': {
            'must': [{'match': {'transcript': 'hello,world!'}}],'filter': [
                {'term': {'tenant_id': 'XRqtv5O91WEEt'}}
            ]
        }
    }
}

解决方法

尝试使用match而不是term

{
    'query': {
        'bool': {
            'must': [{'match': {'transcript': 'hello,world!'}}],'filter': [
                {'match': {'tenant_id': 'XRqtv5O91WEEt'}}
                  ^^^^^
            ]
        }
    }
}

或者,您可以保留term,但改用keyword子字段(如果映射中存在这样的字段)

{
    'query': {
        'bool': {
            'must': [{'match': {'transcript': 'hello,'filter': [
                {'term': {'tenant_id.keyword': 'XRqtv5O91WEEt'}}
                                    ^^^^^^^^
            ]
        }
    }
}