fielddata
大多數字段默認情況下都會建立索引方便查詢,但是針對排序,聚合以及腳本訪問字段值則需要另外的訪問方式;
查詢操作需要回答’哪些doc包含查詢的詞’,而排序和聚合則需要回答’doc中該字段的值是多少’;
大多數字段可以通過文檔索引在磁盤上默認設置的doc_values進行數據訪問,但是text字段不支持doc_values;
text取而代之的是使用查詢時內存數據結構–fielddata,該結構在將字段用於聚合、排序或腳本中時構建;其通過從磁盤中讀取每個段的整個反向索引,然後反轉分詞與文檔的關係並將結果存儲在jvm堆內存當中;
fielddata在text字段中默認未啓用
因爲fielddata會消耗大量的堆內存,特別是當加載大量的text字段時;fielddata一旦加載到堆中,在segment的生命週期之內都將一致保持在堆中,另外加載fielddata也是一個比較耗時的過程,這可能會導致用戶遇到延遲,故而默認情況下禁用fielddata參數;
若嘗試針對text字段進行排序、聚合或腳本操作時,將會拋出以下異常:
PUT param_fielddata_index
{
"mappings": {
"properties": {
"field_data":{
"type": "text"
}
}
}
}
PUT param_fielddata_index/_doc/1
{
"field_data":"aaa"
}
PUT param_fielddata_index/_doc/2
{
"field_data":"bbb"
}
PUT param_fielddata_index/_doc/3
{
"field_data":"ccc"
}
GET param_fielddata_index/_search
{
"sort": [
{
"field_data": {
"order": "desc"
}
}
]
}
可能會出現的異常信息:
Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [field_data] in order to load field data by uninverting the inverted index. Note that this can use significant memory.
在考慮配置fielddata參數之前需要考慮這樣做是否值得,在一般的text字段問題處理中是針對text字段額外映射一個keyword字段,使用keyword字段來進行排序、聚合和腳本操作;
PUT param_fielddata_index/_mapping
{
"properties":{
"field_data":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
}
}
}
}
若考慮再三確實需要配置fielddata,參考以下:
PUT param_fielddata_index
{
"mappings": {
"properties": {
"field_data":{
"type": "text",
"fielddata": true
}
}
}
}
fielddata過濾可減少加載到內存中的分詞量,從而減少內存的使用;
頻率過濾允許加載分詞相關文檔頻率介於最小和最大值之間,數值可以使用精確值或者百分比表示,頻率是按segment計算的,百分比是基於具有該字段值的文檔數量,而不是segment中的所有文檔;
通過使用min_segment_size指定該segment包含的最小文檔數就可以排除掉小的segment:
PUT param_fielddata_index
{
"mappings": {
"properties": {
"field_data":{
"type": "text",
"fielddata": true,
"fielddata_frequency_filter": {
"min":0.001,
"max":0.1,
"min_segment_size":500
}
}
}
}
}
format
用於指定字段類型爲date的格式化形式
ignore_above
用於指定類型(字符串或數組)長度超過參數指定大小時不會建立索引或存儲,該參數可以通過api進行修改,修改後的只會對新增的生效,之前已生成的文檔不會生效(除非進行更新);
//指定message字段爲keyword類型且其ignore_above長度限制20個字符
PUT param_ignore_above_index
{
"mappings": {
"properties": {
"message":{
"type": "keyword",
"ignore_above": 20
}
}
}
}
//文檔會建立索引,message字符少於20同樣會建立索引
PUT /param_ignore_above_index/_doc/1
{
"message":"Syntax error"
}
//文檔會建立索引,但是message字段不會建立索引
PUT param_ignore_above_index/_doc/2
{
"message":"Syntax error with some long stacktrace"
}
//可正常查詢兩條記錄,但是聚合查詢中只會有一條命中
GET param_ignore_above_index/_search
{
"aggs": {
"message": {
"terms": {
"field": "message",
"size": 10
}
}
}
}
ignore_malformed
默認情況下嘗試將錯誤的數據類型索引到字段中會引發異常,整個文檔也將無法建立索引; 若將ignore_malformed參數置爲true,則可忽略類型不匹配造成的異常,不匹配的字段不會建立索引,其他的字段會被正常處理;
PUT param_ignore_malformed_index
{
"mappings": {
"properties": {
"age":{
"type": "integer",
"ignore_malformed": true
},
"level":{
"type": "integer"
}
}
}
}
//正常創建,age字段不會建立索引
PUT param_ignore_malformed_index/_doc/1
{
"text":"test ignore_malformed",
"age":"foo"
}
//創建異常
PUT param_ignore_malformed_index/_doc/2
{
"text":"test ignore_malformed",
"level":"foo"
}
//標記ignore_malformed的字段因爲不建立索引,故而查詢也是不被允許的
//異常信息:number_format_exception
GET param_ignore_malformed_index/_search
{
"query": {
"term": {
"age": {
"value": "foo"
}
}
}
}
ignore_malformed參數可以在以下類型字段上配置
類型 | 相關類型值 |
---|---|
numeric | long,integer,short,byte,double,float,half_float,scaled_float |
date | date,date_nanos |
geo | geo_point,geo_shape |
ip | IPv4,IPv6 |
索引級別的參數設置
可以在索引上設置index.mapping.ignore_malformed參數,在全局上忽略類型不匹配的字段,但是若在具體字段上設置了ignore_malformed參數,則會重載全局設置(以字段設置爲準);
//全局配置ignore_malformed參數,若字段明確指定該參數則以字段上的配置爲準
PUT param_global_ignore_malformed_index
{
"settings": {
"index.mapping.ignore_malformed":true
},
"mappings": {
"properties": {
"age":{
"type": "byte"
},
"level":{
"type": "integer",
"ignore_malformed": false
}
}
}
}
//查看字段映射
GET param_global_ignore_malformed_index/_mapping
//正常,age字段ignore_malform參數使用全局定義
PUT param_global_ignore_malformed_index/_doc/1
{
"text":"global ignore malformed param setting",
"age":"foo"
}
//報錯,level字段ignore_malform參數取字段上定義
PUT param_global_ignore_malformed_index/_doc/2
{
"text":"global ignore malformed param setting",
"level":"foo"
}
不可使用ignore_malformed參數的類型:
1)、nested類型;
2)、object類型;
3)、range類型;
//報錯,Mapping definition for [limit] has unsupported parameters: [ignore_malformed : true]
PUT param_ignore_malformed_limit_index
{
"mappings": {
"properties": {
"limit":{
"type": "object",
"ignore_malformed": true
}
}
}
}
index
index參數控制字段是否會建立索引,接受true/false值,默認爲true,置爲false情況下將無法進行查詢
PUT param_index_set_index
{
"mappings": {
"properties": {
"limit":{
"type": "keyword",
"index": false
}
}
}
}
PUT /param_index_set_index/_doc/1
{
"limit":"index param test"
}
//報錯,Cannot search on field [limit] since it is not indexed.
GET param_index_set_index/_search
{
"query": {
"term": {
"limit": {
"value": "index param test"
}
}
}
}
index_options
該參數用於控制將那些信息添加到倒排索引以進行搜索和高亮顯示,index_options參數僅適用於text字段,應避免該參數與其他類型字段一起使用;
該參數只是以下參數:
類型 | 說明 |
---|---|
docs | 僅對文檔編號建立索引,可以回答’這個詞是否在這個字段當中存在’的問題 |
freqs | 對文檔編號和分詞頻率建立索引,分詞頻率用於計算分詞相關度分數(分詞重複次數越多,其分數將越高) |
positions | 默認值,對文檔編號、分詞頻率及分詞位置建立索引,可用於短語查詢 |
offsets | 對文檔編號、分詞頻率、分詞位置及分詞起始位置(用於將分詞字符串映射回原始字符串)建立索引,可用於加速高亮顯示的查詢 |
tips:詞元位置與分詞元始位置區別:文檔在經過分詞器切分之後產生多個詞元,詞元位置是相對其他詞元而言的,而起始位置則是相對文檔中所有詞而言的;
//不支持phraseQuery
PUT param_index_options_index_1
{
"mappings": {
"properties": {
"text":{
"type": "text",
"index_options": "docs"
}
}
}
}
PUT param_index_options_index_1/_doc/1
{
"text":"hello world"
}
GET param_index_options_index_1/_search
{
"query": {
"match": {
"text": "hello world"
}
},
"highlight": {
"fields": {
"text": {}
}
}
}
PUT param_index_options_index_2
{
"mappings": {
"properties": {
"text":{
"type": "text",
"index_options": "freqs"
}
}
}
}
PUT param_index_options_index_2/_doc/1
{
"text":"hello world"
}
//報錯,freqs不支持PhraseQuery,field:[text] was indexed without position data; cannot run PhraseQuery
GET param_index_options_index_2/_search
{
"query": {
"match_phrase": {
"text": "hello world"
}
}
}
GET param_index_options_index_2/_search
{
"query": {
"match": {
"text": "hello world"
}
}
}
PUT param_index_options_index_3
{
"mappings": {
"properties": {
"text":{
"type": "text",
"index_options": "positions"
}
}
}
}
PUT param_index_options_index_3/_doc/1
{
"text":"hello world"
}
GET param_index_options_index_3/_search
{
"query": {
"match": {
"text": "hello world"
}
},
"highlight": {
"fields": {
"text": {}
}
}
}
PUT param_index_options_index_4
{
"mappings": {
"properties": {
"text":{
"type": "text",
"index_options": "offsets"
}
}
}
}
PUT param_index_options_index_4/_doc/1
{
"text":"hello world"
}
GET param_index_options_index_4/_search
{
"query": {
"match": {
"text": "hello world"
}
},
"highlight": {
"fields": {
"text": {}
}
}
}
index_phrases
該參數可以將兩個詞元(term)組合成一個詞組映射到單獨字段中,接受true/false,默認爲false,這可以使精確的短語查詢更有效地運行(需要額外付出索引的代價);
另外這參數最好要在不刪除停用詞的情況下使用,因爲包含停止詞的短語將不會使用該組合字段並退而使用標準短語查詢;
index_prefixes
index_prefixed參數可對詞元進行前綴索引以提升詞元前綴查詢速度,可選以下參數:
類型 | 說明 |
---|---|
min_chars | 最小索引字符長度,必須大於0,默認爲2(包含) |
max_chars | 最大索引字符長度,必須小於20,默認爲5(包含) |
PUT param_index_prefix_index
{
"mappings": {
"properties": {
"text":{
"type": "text",
"index_prefixes":{
"min_chars":2,
"max_chars":10
}
}
}
}
}
PUT param_index_prefix_index/_doc/1
{
"text":"Once the RestClient has been created, requests can be sent by calling either performRequest or performRequestAsync"
}
PUT param_index_prefix_index/_doc/2
{
"text":"performRequest is synchronous and will block the calling thread and return the Response when the request is successful or throw an exception if it fails"
}
GET param_index_prefix_index/_search
{
"query": {
"prefix": {
"text": {
"value": "perform"
}
}
}
}
//報錯,當prefix的字符長度低於min_chars報null_pointer_exception
GET param_index_prefix_index/_search
{
"query": {
"prefix": {
"text": {
"value": "p"
}
}
}
}
//當prefix的字符長度高於max_chars則無法查詢出結果,返回結果爲空
GET param_index_prefix_index/_search
{
"query": {
"prefix": {
"text": {
"value": "performRequest"
}
}
}
}