这里主要测试(text/keyowrd)在(term/match)搜索过程中的区别。
当一个文档被索引时,每个field(字段)都可能创建一个倒排索引,当然,也可以在创建mapping的时候,指定不索引该field。
我们用以下mapping创建索引 test1
PUT test1
{
"mappings": {
"properties": {
"text_col": {
"type": "text"
},
"keyword_col": {
"type": "keyword"
}
}
}
}
如需指定字段不进行索引:
PUT test2
{
"mappings": {
"properties": {
"unindex_col": {
"type": "keyword",
"index": false
}
}
}
}
创建好test1后,写入一条记录,作为后面搜索测试用:
POST /test1/_doc/
{
"text_col": "hello world 你好世界",
"keyword_col": "hello world 你好世界"
}

当写入记录后,在text_col字段上会创建一个倒排索引 ["hello", "world", "你", "好", "世", "界"]
可用如下方式查看默认使用的分词器的分词结果:
GET /_analyze
{
"analyzer": "standard",
"text": "hello world 你好世界"
}

而 keyword_col 不进行分词,使用二进制原样存储为 ["hello world 你好世界"]
当然,也可以不使用默认的分词器,可以指定其它分词器,例:中文分词器。
Elasticsearch中的term和match查询:文本与keyword字段差异,

本文探讨了Elasticsearch中text和keyword字段在term和match查询中的行为差异,涉及倒排索引、分词与非分词的处理,以及精确查询与模糊查询的区别。
最低0.47元/天 解锁文章
6678





