悟空,拿我的打狗棒来

es5.4用DSL查询的时候大小写敏感查询

Elasticsearch | 作者 bedman | 发布于2019年04月15日 | 阅读数:3057

一张图是分词的结果,一张是查询的结果。发现用is或者Is都能查询的到。有办法做到查询的时候区分大小写敏感查询吗。
查询结果.png 分词结果.png
已邀请:

printf_uck - 1024

赞同来自:

analyzer默认做了统一大小写,你如果要区分大小写,单独设置一个分词,查询的时候也带着这个分词器去查

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

Lowercase Tokenizer就是区分大小写的 推荐自定义分词器加上它

bedman - 90hou

赞同来自:

我按照两位的说法测试了下,查询的时候用小写仍然查询不到,我贴一下我的配置两位帮分析一下,
mapping(新增了一个analyzer为”my_analyzer_forsearch“):{
    "lower_index111": {
        "aliases": {},
        "mappings": {
            "doc": {
                "dynamic_templates": [
                    {
                        "strings_analyzed": {
                            "match_mapping_type": "string",
                            "mapping": {
                                "analyzer": "my_analyzer",
                                "index_options": "offsets",
                                "search_analyzer": "my_analyzer_forsearch",
                                "type": "text"
                            }
                        }
                    },
                    {
                        "strings_not_analyzed": {
                            "match_mapping_type": "string",
                            "mapping": {
                                "type": "keyword"
                            }
                        }
                    }
                ],
                "properties": {
                    "value": {
                        "type": "text",
                        "index_options": "offsets",
                        "analyzer": "my_analyzer",
                        "search_analyzer": "my_analyzer_forsearch"
                    }
                }
            },
            "_default_": {
                "dynamic_templates": [
                    {
                        "strings_analyzed": {
                            "match_mapping_type": "string",
                            "mapping": {
                                "analyzer": "my_analyzer",
                                "index_options": "offsets",
                                "search_analyzer": "my_analyzer_forsearch",
                                "type": "text"
                            }
                        }
                    },
                    {
                        "strings_not_analyzed": {
                            "match_mapping_type": "string",
                            "mapping": {
                                "type": "keyword"
                            }
                        }
                    }
                ]
            }
        },
        "settings": {
            "index": {
                "number_of_shards": "5",
                "provided_name": "lower_index111",
                "creation_date": "1555382095182",
                "analysis": {
                    "analyzer": {
                        "my_analyzer": {
                            "filter": [
                                "asciifolding"
                            ],
                            "type": "custom",
                            "tokenizer": "standard"
                        },
                        "my_analyzer_forsearch": {
                            "filter": [
                                "asciifolding"
                            ],
                            "type": "custom",
                            "tokenizer": "lowercase"
                        }
                    }
                },
                "number_of_replicas": "1",
                "uuid": "kNi6FW2ZSz-fbxAm5xWDtg",
                "version": {
                    "created": "5040199"
                }
            }
        }
    }
}
 
这个是分词结果(为大写”Is“):
{
    "_index": "lower_index111",
    "_type": "doc",
    "_id": "1",
    "_version": 1,
    "found": true,
    "took": 0,
    "term_vectors": {
        "value": {
            "field_statistics": {
                "sum_doc_freq": 4,
                "doc_count": 1,
                "sum_ttf": 4
            },
            "terms": {
                "Is": {
                    "term_freq": 1,
                    "tokens": [
                        {
                            "position": 0,
                            "start_offset": 0,
                            "end_offset": 2
                        }
                    ]
                },
                "an": {
                    "term_freq": 1,
                    "tokens": [
                        {
                            "position": 2,
                            "start_offset": 8,
                            "end_offset": 10
                        }
                    ]
                },
                "apple": {
                    "term_freq": 1,
                    "tokens": [
                        {
                            "position": 3,
                            "start_offset": 11,
                            "end_offset": 16
                        }
                    ]
                },
                "this": {
                    "term_freq": 1,
                    "tokens": [
                        {
                            "position": 1,
                            "start_offset": 3,
                            "end_offset": 7
                        }
                    ]
                }
            }
        }
    }
}
这个是查询条件,用的小写”is“,分词器指定了”my_analyzer_forsearch“:
{
    "query": {
        "bool": {
            "must": [{
                "query_string": {
                    "query": "is",
                    "analyze_wildcard": true,
                    "default_operator":"AND",
                    "analyzer":"my_analyzer_forsearch"
                }
            }],
            "must_not": []
        }
    }
}
 

要回复问题请先登录注册