#删除索引tt
DELETE tt
#新建全局属性
#其中filter中word_delimiter分词模式在数字加英文字母(34324aa)时会插入异常,解决方案为:去除word_delimiter分词模式
#word_delimiter:将分词一个字一个字拆分
PUT tt
{
"settings": {
"analysis": {
"analyzer": {
"ik_smart_pinyin": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": ["my_pinyin", "word_delimiter"]
},
"ik_max_word_pinyin": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["my_pinyin", "word_delimiter"]
}
},
"filter": {
"my_pinyin": {
"type": "pinyin",
"keep_separate_first_letter": false,
"keep_first_letter": false,
"keep_full_pinyin": false,
"keep_joined_full_pinyin":true,
"keep_original": false,
"limit_first_letter_length": 16,
"lowercase": true,
"remove_duplicated_term": true
}
}
}
}
}
#查询全局属性
GET tt/_settings
#新增映射关系
POST tt/entry/_mapping
{
"entry": {
"properties": {
"author": {
"type": "keyword",
"store": true
},
"content": {
"type": "text",
"store": true,
"analyzer": "ik_max_word_pinyin"
},
"createTime": {
"type": "date",
"store": true,
"format": "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd"
},
"fondsId": {
"type": "keyword",
"store": true
},
"mgtLevel": {
"type": "keyword",
"store": true
},
"nodeId": {
"type": "keyword",
"store": true
},
"other": {
"type": "text",
"store": true
},
"tag": {
"type": "text",
"store": true
},
"title": {
"type": "text",
"store": true,
"analyzer": "ik_max_word_pinyin"
},
"updateTime": {
"type": "date",
"store": true,
"format": "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd"
},
"view": {
"type": "text",
"store": true
}
}
}
}
#查询映射关系
GET tt/_mapping/entry
#插入id为1的数据
PUT tt/entry/1
{
"id":106996,
"title":"431101199607116477(431101199607116477)",
"content":": I2",
"fondsId":299
}
#模糊查询title中有‘程咬金’的文档(高亮)
GET archives/entry/_search
{
"query": {
"bool": {
"must_not": [
{"match": {
"title": "程咬金"
}}
]
}
},
"highlight": {
"require_field_match": "false",
"fields": {
"title": {},
"content": {}
}
}
}
#分页查询,且should查询内容必须出现一次
#minimum_should_match:此属性为should查询的条件成立次数,紧跟should查询之后,默认为0
GET archives/entry/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"filter": [{
"terms": {
"fondsId": ["299"],
"boost": 1.0
}
}],
"should": [{
"multi_match": {
"query": "ceshi",
"fields": ["content^1.0",
"title^1.0"],
"type": "best_fields",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1.0
}
},
{
"multi_match": {
"query": "数据",
"fields": ["content^1.0",
"title^1.0"],
"type": "best_fields",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1.0
}
}],
"adjust_pure_negative": true,
"minimum_should_match": "1",
"boost": 1.0
}
},
"highlight": {
"pre_tags": ["<h2>"],
"post_tags": ["</h2>"],
"fragment_size": 127,
"number_of_fragments": 1,
"no_match_size": 127,
"fields": {
"title": {
},
"content": {
}
}
}
}
#多字段模糊查询及单字段多值精确过滤(高亮)
#建议采用此种查询方式
GET tt/entry/_search
{
"query": {
"bool": {
"should": [
{"multi_match": {
"query": "程咬金",
"fields": ["title","content"]
}}
],
"minimum_should_match":1,
"filter": {"terms": {
"fondsId": [
"299"
]
}}
}
},
"highlight": {
"require_field_match": "false",
"fields": {
"title": {},
"content": {}
}
}
}
#查询全部
GET tt/entry/_search
#根据id查询
GET tt/entry/107152
#测试ik_max_word查询结果
GET tt/_analyze
{
"text":"测试数据",
"analyzer":"ik_max_word"
}
#测试ik_smart查询结果
GET tt/_analyze
{
"text":"测试数据",
"analyzer":"ik_smart"
}