分词field+fielddata的工作原理
doc value
--> 不分词的所有field,可以执行聚合操作
--> 如果你的某个field不分词,那么在index-time,就会自动生成doc value
--> 针对这些不分词的field执行聚合操作的时候,自动就会用doc value来执行
分词field,是没有doc value的。在index-time,
如果某个field是分词的,那么是不会给它建立doc value正排索引的,因为分词后,占用的空间过于大,所以默认是不支持分词field进行聚合的
分词field默认没有doc value,所以直接对分词field执行聚合操作,是会报错的
1、对于分词field,必须打开和使用fielddata,
2、完全存在于纯内存中。结构和doc value类似。如果是ngram或者是大量term,那么必将占用大量的内存。
3、如果一定要对分词的field执行聚合,那么必须将fielddata=true,然后es就会在执行聚合操作的时候,现场将field对应的数据,建立一份fielddata正排索引,fielddata正排索引的结构跟doc value是类似的,但是只会讲fielddata正排索引加载到内存中来,然后基于内存中的fielddata正排索引执行分词field的聚合操作
4、如果直接对分词field执行聚合,报错,才会让我们开启fielddata=true,告诉我们,会将fielddata uninverted index,正排索引,加载到内存,会耗费内存空间
5、为什么fielddata必须在内存.因为分词的字符串,需要按照term进行聚合,需要执行更加复杂的算法和操作,如果基于磁盘和os cache,那么性能会很差
1、对于分词的field执行aggregation,发现报错。
GET /test_index/test_type/_search
{
"aggs": {
"group_by_test_field": {
"terms": {
"field": "test_field"
}
}
}
}
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [test_field] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test_index",
"node": "4onsTYVZTjGvIj9_spWz2w",
"reason": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [test_field] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
}
}
],
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [test_field] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
}
},
"status": 400
}
设置 "fielddata": true
POST /test_index/_mapping/test_type
{
"properties": {
"test_field": {
"type": "text",
"fielddata": true
}
}
}
{
"test_index": {
"mappings": {
"test_type": {
"properties": {
"test_field": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"fielddata": true
}
}
}
}
}
}
查询:
GET /test_index/test_type/_search
{
"size": 0,
"aggs": {
"group_by_test_field": {
"terms": {
"field": "test_field"
}
}
}
}
查询结果:
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_test_field": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "test",
"doc_count": 2
}
]
}
}
}
使用内置field keyword不分词,对string field进行聚合
GET /test_index/test_type/_search
{
"size": 0,
"aggs": {
"group_by_test_field": {
"terms": {
"field": "test_field.keyword"
}
}
}
}
结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_test_field": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "test",
"doc_count": 2
}
]
}
}
}
ElasticSearch 笔记
1_ElasticSearch使用term filter来搜索数据
2_ElasticSearch filter执行原理 bitset机制与caching机制
3_ElasticSearch 基于bool组合多个filter条件来搜索数据
4_ElasticSearch 使用terms搜索多个值
5_ElasticSearch 基于range filter来进行范围过滤
6_ElasticSearch 控制全文检索结果的精准度
7_ElasticSearch term+bool实现的multiword搜索原理
8_基于boost的搜索条件权重控制
9_ElasticSearch 多shard场景下relevance score不准确
10_ElasticSearch dis_max实现best fields策略进行多字段搜索
11_ElasticSearch 基于tie_breaker参数优化dis_max搜索效果
12_ElasticSearch multi_match语法实现dis_max+tie_breaker
13_ElasticSearch multi_match+most fiels策略进行multi-field搜索
14_ElasticSearch 使用most_fields策略进行cross-fields search
15_ElasticSearch copy_to定制组合field进行cross-fields搜索
16_ElasticSearch 使用原生cross-fiels 查询
17_ElasticSearch phrase matching搜索
18_ElasticSearch 基于slop参数实现近似匹配
19_ElasticSearch 使用match和近似匹配实现召回率与精准度的平衡
20_ElasticSearch rescoring机制优化近似匹配搜索的性能
21_ElasticSearch 前缀搜索、通配符搜索、正则搜索
22_ElasticSearch 搜索推荐match_phrase_prefix实现search-time
23_ElsaticSearch 搜索推荐ngram分词机制实现index-time更多干货
24_ElasticSearch TF&IDF算法以及向量空间模型
25_ElasticSearch 揭秘lucene的相关度分数算法
26_ElasticSearch 四种常见的相关度分数优化方法
27_ElasticSearch用function_score自定义相关度分数算法
28_ElasticSearch误拼写时的fuzzy模糊搜索技术
29_ElasticSearchIK中文分词器的安装和使用
30_ElasticSearch IK分词器配置文件 以及自定义词库
ElasticSearchIK中文分词器的安装和使用
日志管理ELK