命令 GET index/type/_mapping 查看某一索引的mapping
個人比較喜歡 將mapping 單獨寫一個文件,而不是將注解寫在bean的屬性上
比如新建一個Product 的bean對象 可以這么寫:
1.
@Document(indexName = "index_product" ,type = “index_product”,shards = 1,replicas =2)
public class Product{
@Id
private String id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String productName;
@Field(index = false, type = FieldType.Keyword)
private String productInfo;
....
}
2.
也可以寫在mapping里面,如下:
@Mapping(mappingPath="/XXX/XXX/index_product_mapping.json")
@Document(indexName = "index_product" ,type = “index_product”,shards = 1,replicas =2)
public class Product{
@Id
private String id;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String productName;
@Field(index = false, type = FieldType.Keyword)
private String productInfo;
....
}
index_product_mapping.json文件
{
"index_product":{
"properties":{
"id":{
"type":"text"
}
“productInfo”:{
"type":"text",
"index": false #index參數作用是控制當前字段是否被索引,默認為true,false表示不記錄,即不可被搜索,
}
}
}
}
如果index是false,則不能被搜索,
GET index_product/index_product/_search { "query": { "match": { "productInfo": "111" } } } 則會報錯
數據類型:
核心數據類型:
字符串型: text、keyword(不會分詞)
數值型: long、integer、short、byte、double、float、half_float等
日期類型: date
布爾類型: boolean
二進制類型: binary
范圍類型: integer_range、float_range、long_range、double_range、date_range
復雜數據類型:
數組類型:array
對象類型:object
嵌套類型:nested object
地理位置數據類型:geo_point、geo_shape
專用類型:ip(記錄ip地址)、completion(實現自動補全)、token_count(記錄分詞數)、murmur3(記錄字符串hash值
multi-fields
多字段特性
允許對同一個字段采用不同的配置,比如分詞,常見的例子如對人名實現拼音搜索,只需要在人名中新增一個子字段為pinyin即可。
PUT myindex1
{
"mappings": {
"doc": {
"properties": {
"username": {
"type":"text",
"fields": {
"pinyin": {
"type": "text",
"analyzer": "pinyin"
}
}
}
}
}
}
}
GET myindex1/_search
{
"query": {
"match": {
"username.pinyin": "hanhan"
}
}
}
通過dynamic
參數來控制字段的新增
true:
默認值,允許自動新增字段false:
不允許自動新增字段,但是文檔可以正常寫入,但無法對字段進行查詢等操作strict:
文檔不能寫入,報錯
#定義索引,定義title、name、age三個字段類型,對於其他新增字段dynamic設置為false
PUT myindex
{
"mappings": {
"doc": {
"dynamic": false,
"properties": {
"title": {
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}
#查看剛才自定義的mapping
GET myindex/_mapping
#索引一條文檔,字段title、desc,其中desc為新增字段
PUT myindex/doc/1
{
"title": "hello world",
"desc": "nothing"
}
#使用title字段查詢,一切正常
GET myindex/_search
{
"query": {
"match": {
"title": "hello"
}
}
}
#無法使用desc字段進行查詢,返回為0
GET myindex/_search
{
"query": {
"match": {
"desc": "nothing"
}
}
}
index
:控制當前字段是否索引,默認為true,即記錄索引,false不記錄,即不可搜索
PUT myindex
{
"mappings": {
"doc": {
"properties": {
"cookie": {
"type": "text",
"index":false
}
}
}
}
}
#使用cookie字段查詢會報錯
index_options:用於控制倒排索引記錄的內容,有如下4種配置
docs: 只記錄doc id
freqs: 記錄doc id 和term frequencies
positions: 記錄doc id、term frequencies和term position
offsets: 記錄doc id、term frequencies、term position和character offsetstext
類型默認配置為positions
,其他默認為docs
記錄內容越多,占用空間越大。
PUT myindex1
{
"mappings": {
"doc": {
"properties": {
"cookie": {
"type": "text",
"index_options": "offsets"
}
}
}
}
}
null_value
: 當字段遇到null值時的處理策略,默認為null,即空值,此時es會忽略該值。可以通過設定該值設定字段的默認值
PUT myindex1
{
"mappings": {
"doc": {
"properties": {
"status_code": {
"type": "keyword",
"null_value": "NULL"
}
}
}
}
}
動態字段映射