1、建es索引
#这里指定了分词器为ngram_filter
PUT db_content_testserver2
{
"settings": {
"number_of_shards": "3",
"index": {
"max_ngram_diff": 10
},
"analysis": {
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"char_filter": [],
"tokenizer": "keyword",
"filter": [
"ngram_filter"
]
}
}
,
"filter": {
"ngram_filter": {
"type": "ngram",
"min_gram": 2,
"max_gram": 5
}
}
},
"number_of_replicas": "1"
}
}
2、获取es索引的结构
GET /索引名
例如:GET /db_content_testserver2
返回结果如下
{
"db_content_testserver2" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"max_ngram_diff" : "10",
"number_of_shards" : "3",
"provided_name" : "db_content_testserver2",
"creation_date" : "1622776615704",
"analysis" : {
"filter" : {
"ngram_filter" : {
"type" : "ngram",
"min_gram" : "2",
"max_gram" : "5"
}
},
"analyzer" : {
"ngram_analyzer" : {
"filter" : [
"ngram_filter"
],
"char_filter" : [ ],
"type" : "custom",
"tokenizer" : "keyword"
}
}
},
"number_of_replicas" : "1",
"uuid" : "YLomhtQTQfKBF0zSBudwLg",
"version" : {
"created" : "7020199"
}
}
}
}
}
3、删除es索引
DELETE /索引名
例如:DELETE /db_content_testserver2
4、测试索引的分词
POST /db_content_testserver2/_analyze
{
"text": "国家地区及国内省份青岛",
"analyzer": "ngram_analyzer"
}
5、创建字段
#创建content字段,指定分词为ngram_analyzer分词
PUT db_content_testserver2/_mappings/
{
"properties": {
"content": {
"type": "text",
"analyzer": "ngram_analyzer"
}
}
}
6、写入测试数据(失败,后续再查询脚本)
POST /db_content_testserver2/
{
"content":"国家地区及国内省份青岛"
}
7、检索某个字段
#在索引dws_lizhi_content_audio_info_da中keywords_string字段中搜索包含“国家”的记录
GET /dws_lizhi_content_audio_info_da/_search
{
"query": {
"match_phrase": {
"keywords_string": {
"query":"国家",
"slop": 6
}
}
}
}
8、查询索引的全部内容
GET user_profile_pop_up_da/_search #查全部
9、通过主键查询
GET user_profile_pop_up_da/_doc/868822038468110_1 #查某一个主键的数据