一、新建index
1.建立index(設(shè)置analyzer)
PUT /commodity_v2
{
"index": {
"analysis": {
"analyzer": {
"by_smart": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": ["by_tfr","by_sfr"],
"char_filter": ["by_cfr"]
},
"by_max_word": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["by_tfr","by_sfr"],
"char_filter": ["by_cfr"]
}
},
"filter": {
"by_tfr": {
"type": "stop",
"stopwords": [" "]
},
"by_sfr": {
"type": "synonym",
"synonyms_path": "analysis/synonyms.txt"
}
},
"char_filter": {
"by_cfr": {
"type": "mapping",
"mappings": ["| => |"]
}
}
}
}
}
2.設(shè)置mapping
PUT /commodity_v2/_mapping/search
{
"properties": {
"channel": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"commodityId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"commodityName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"index": "analyzed",
"analyzer": "by_max_word",
"search_analyzer": "by_smart"
},
"commodityType": {
"type": "long"
},
"endTime": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"inventory": {
"type": "long"
},
"keywords": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"index": "analyzed",
"analyzer": "by_max_word",
"search_analyzer": "by_smart"
},
"price": {
"type": "long"
},
"publishTime": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"salesVolume": {
"type": "long"
},
"startTime": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tagArray": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
3.復(fù)制原index內(nèi)數(shù)據(jù)到新index
POST _reindex //將commodity內(nèi)的文檔復(fù)制到commodity_v2中,默認(rèn)同樣字段名稱匹配
{
"source": {
"index": "commodity"
},
"dest": {
"index": "commodity_v2"
}
}
4.查詢index內(nèi)現(xiàn)有mapping
GET /commodity_v2/_mapping?pretty
5.驗(yàn)證是否同義詞生效
POST /commodity_v2/_search
{
"query" : {
"match_phrase" : {
"commodityName" : "接近無脂"
}
}
}
二、其他查詢
1.查詢所有index
GET /_cat/indices?v
2.search
a.查詢兩字段(兩字段為or的關(guān)系)
GET /commodity_v2/_search
{
"query": {
"multi_match": {
"query": "空調(diào)",
"fields": [ "commodityName", "keywords" ]
}
}
}
b.查詢兩字段,設(shè)置權(quán)重
POST /commodity_v2/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"commodityName": {
"query": "空調(diào)",
"boost": 3
}
}
},
{
"match": {
"keywords": "空調(diào)"
}
}
]
}
}
}
c.(A or B)and C
POST /commodity_v2/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"commodityName": {
"query": "空調(diào)",
"boost": 3
}
}
},
{
"match": {
"keywords": "空調(diào)"
}
}
]
}
},
{
"match_phrase": {
"tagArray": "TG171018131900000"
}
}
]
}
}
}
3.設(shè)置別名
1.移除舊的別名關(guān)聯(lián),建立新的
POST /_aliases
{
"actions": [
{ "remove": { "index": "my_index_v1", "alias": "my_index" }},
{ "add": { "index": "my_index_v2", "alias": "my_index" }}
]
}
2.查詢別名
GET /*/_alias/my_index //查某個(gè)別名映射的所有index
GET /my_index_v1/_alias/* //查詢某個(gè)index擁有的別名