ignore_above 默認(rèn)值是256和敬,該參數(shù)的意思是凹炸,當(dāng)字段文本的長度大于指定值時(shí),不做倒排索引昼弟。
也就是說啤它,當(dāng)字段文本的長度大于指定值時(shí),聚合舱痘、全文搜索都查不到這條數(shù)據(jù)变骡。ignore_above 最大值是32766,但是要根據(jù)場景來設(shè)置芭逝,比如說中文最大值應(yīng)該是設(shè)定在10922塌碌。
ignore_above 背后實(shí)際的含義是,Lucene對一個(gè)文本的解析長度旬盯,當(dāng)這個(gè)長度大于32766時(shí)台妆,將不會落實(shí)analyze行為。
Elasticsearch中采用的是字符個(gè)數(shù)來定義igmore_above值, 而
lucene是采用byte字節(jié)瓢捉;那么每個(gè)象形文字在utf-8中占位是3,
每個(gè)Literal字符在utf-8中占位是2, 每個(gè)ASCII字符在utf-8中占位是1.象形文字字符(中文频丘、韓文、日文): 10922 個(gè)字符(算法是: 32766 / 3).
Literal字符(印度問泡态、俄文): 16383 個(gè)字符(算法是: 32766 / 2).
ASCII字符(a-zA-Z0-9以及~!@#$等特殊字符): 32766個(gè)字符(算法是: 32766).
?
測試代碼
from elasticsearch import Elasticsearch
from pprint import pprint
import time
es = Elasticsearch(hosts=["192.168.1.100"])
# 建索引, 寫入數(shù)據(jù).
if not es.indices.exists("my_index"):
es.indices.create(
index="my_index",
body={
"mappings": {
"my_type": {
"properties": {
"message": {
"type": "keyword",
"ignore_above": 20
}
}
}
}
}
)
# message: 小于20: 被倒排索引搂漠,能聚合、能被搜索某弦。
s1 = es.index(
index="my_index",
doc_type="my_type",
id=1,
body={
"message": "Syntax error"
}
)
# message: 等于19: 被倒排索引桐汤,能聚合、能被搜索靶壮。
s2 = es.index(
index="my_index",
doc_type="my_type",
id=3,
body={
"message": "Syntax error search"
}
)
# message: 等于20 被倒排索引怔毛,能聚合、能被搜索腾降。
s2 = es.index(
index="my_index",
doc_type="my_type",
id=4,
body={
"message": "Syntax error elastic"
}
)
# message: 大于20 沒有倒排索引拣度,不能聚合、不能被搜索,可以被空搜索抗果。
s3 = es.index(
index="my_index",
doc_type="my_type",
id=2,
body={
"message": "Syntax error with some long stacktrace"
}
)
# message: 象形文字, 20個(gè)文字. 被倒排索引筋帖,能聚合、能被搜索冤馏。
es.index(
index="my_index",
doc_type="my_type",
id=10,
body={
"message": "中國象形文字博大精深喔不知道你是否感興趣"
}
)
# message: 象形文字, 21個(gè)文字. 沒有倒排索引日麸,不能聚合、不能被搜索逮光,可以被空搜索代箭。
es.index(
index="my_index",
doc_type="my_type",
id=11,
body={
"message": "中國象形文字博大精深喔不知道你是否感興趣呢"
}
)
# 等待refresh導(dǎo)磁盤(才能搜索)
time.sleep(10)
# ignore_above: 大于數(shù)值(默認(rèn): 256, 本范例: 20)范圍將不被聚合.
s4 = es.search(
index="my_index",
body={
"aggs": {
"messages": {
"terms": {
"field": "message"
}
}
}
}
)
# ignore_above: 大于數(shù)值(默認(rèn): 256, 本范例: 20)范圍將不被搜索到.
s5 = es.search(
index="my_index",
body={
"query": {
"query_string": {
"query": "syntax"
}
}
}
)
# ignore_above: 空搜索可以搜索到.
s6 = es.search(
index="my_index"
)
print("聚合:")
pprint(s4)
print("\n" * 10, "全文搜索: ")
pprint(s5)
print("\n" * 10, "空搜索: ")
pprint(s6)
?
參考
- [x] ignore_above