mapping创建
curl -X PUT "http://localhost:9200/index/_mapping/_doc?pretty" -H 'Content-Type:application/json' -d'
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"keywords": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"utime": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
}
}
}'
获取索引index,type为_doc的mapping
curl -X GET "localhost:9200/index/_mapping/_doc?pretty"
获取插件
curl -X GET "localhost:9200/_cat/plugins?v&s=component&h=name,component,version,description&pretty"
获取索引
curl -X GET "localhost:9200/_cat/indices/?v&s=index&pretty"
http://10.41.41.159:9200/_cat/indices/?v&s=index&pretty
在索引index,type为_doc上创建id=的索引
curl -X PUT http://localhost:9200/index/_doc/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -X PUT http://localhost:9200/index/_doc/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -X PUT http://localhost:9200/index/_doc/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -X PUT http://localhost:9200/index/_doc/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'
curl -X PUT http://localhost:9200/index/_doc/5 -H 'Content-Type:application/json' -d'
{"content":"支付宝我来解绑"}'
curl -X PUT http://localhost:9200/index/_doc/6 -H 'Content-Type:application/json' -d'
{"content":"支付宝好不好解绑"}'
curl -X PUT http://localhost:9200/index/_doc/7 -H 'Content-Type:application/json' -d'
{"content":"我的支付宝你来绑定"}'
curl -X PUT http://localhost:9200/index/_doc/8 -H 'Content-Type:application/json' -d'
{"title":"支付宝","keywords":"第一条","content":"我的支付宝你来绑定"}'
curl -X PUT http://localhost:9200/index/_doc/9 -H 'Content-Type:application/json' -d'
{"title":"第二条","keywords":"解绑","content":"我的支付宝你来绑定"}'
curl -X PUT http://localhost:9200/index/_doc/10 -H 'Content-Type:application/json' -d'
{"title":"第三条","keywords":"第二条关键词","content":"我的支付宝你来绑定"}'
curl -X PUT http://localhost:9200/index/_doc/11 -H 'Content-Type:application/json' -d'
{"title":"解绑","keywords":"第四条","content":"我的支付宝你来绑定"}'
搜索api
curl -X POST http://localhost:9200/index/_search?pretty -H 'Content-Type:application/json' -d'
{
"query" : { "match" : { "content" : "支付宝解绑与绑定" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
bool搜索
curl -X GET "localhost:9200/knowledge_base/_search/1?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"multi_match" : {
"query" : "违规",
"fields" : [ "title^8", "keywords^2","content^1" ]
}
},],
"must" : {
"term" : { "" : "" },
"range" : {
"utime" : { "gte" : 10, "lte" : 20 }
}
},
"filter": {
"term" : { "tag" : "tech" }
},
},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {},
"title" : {},
"keywords" : {}
}
}
}
'
bool+multi match带权重的搜索
curl -X GET "localhost:9200/knowledge_base3/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should":
{
"multi_match" : {
"query" : "违规",
"fields" : [ "title^8", "keywords^2","content^1" ]
}
},
"must" : [
{"term" : { "id" : "22458" }},
{"term" : { "category" : "1"}},
{"term" : { "type1" : "35213"}},
{"term" : { "type2" : "35805"}},
{"term" : { "type3" : "35830"}},
{"term" : { "type4" : "-2"}},
{
"range":{
"utime":{
"gte":"2019-04-25 17:41:28",
"lte":"2019-04-25 17:41:28",
"format":"yyyy-MM-dd HH:mm:ss"
}
}
}
]
}
},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {},
"title" : {},
"keywords" : {}
}
}
}
'
curl -X GET "localhost:9200/knowledge_base/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": {
"includes": ["id", "title", "content", "keywords", "category", "type1", "type2", "type3", "type4"],
"excludes": ["utime"]
},
"query": {
"multi_match": {
"query": "\u652f\u4ed8\u5b9d",
"fields": ["title^8", "keywords^2", "content^1"]
}
},
"highlight": {
"pre_tags": ["<tag1>", "<tag2>"],
"post_tags": ["</tag1>", "</tag2>"],
"fields": {
"content": {},
"title": {},
"keywords": {}
}
},
"from": 0,
"size": 20
}'
curl -X GET "localhost:9200/knowledge_base/_search?pretty" -H 'Content-Type: application/json' -d'
'{"_source":{"includes":["id","title","content","keywords","category","type1","type2","type3","type4"],"excludes":["utime"]},"query":{"multi_match":{"query":"\u7ed1\u5b9a","fields":["title^8","keywords^2","content^1"]}},"highlight":{"pre_tags":["<span style=\"background-color:yellow;\">","<span style=\"background-color:yellow;\">"],"post_tags":["</span>","</span>"],"fields":{"content":{},"title":{},"keywords":{}}},"from":0,"size":20}'
curl -X PUT "http://localhost:9200/knowledge_base/_mapping/back?pretty" -H 'Content-Type:application/json' -d'
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"keywords": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}'
curl -X GET "localhost:9200/knowledge_base/_mapping/back?pretty"
删除索引knowledge_base3
curl -X DELETE "localhost:9200/knowledge_base3?pretty"
curl -X DELETE "localhost:9200/faq?pretty"
给索引加mapping
curl -X PUT "localhost:9200/knowledge_base?pretty" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"back" : {
"properties" : {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"keywords": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
}
'
搜索所有的索引
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
创建mapping
curl -X PUT "localhost:9200/knowledge_base3?pretty" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"back" : {
"properties" : {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"keywords": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"utime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"type4": {
"type": "integer"
}
}
}
}
}
'
curl -X PUT "10.75.13.187:9900/faq?pretty" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 2
},
"mappings" : {
"front" : {
"properties" : {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"keywords": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"status": {
"type": "integer"
},
"location": {
"type": "integer"
}
}
}
}
}
'
删除索引
curl -X DELETE "10.75.13.187:9900/faq?pretty"
用分词器分析查询词
curl -X GET "localhost:9200/knowledge_base/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
"field": "keywords",
"text": "双十一"
}
'
集群状态
http://10.75.13.187:9900/_cluster/state?
索引状态
http://10.75.12.165:9900/_cat/indices?v&s=index&pretty
获取id为20990的文档
curl -X GET "10.75.12.165:9900/faq/front/20990?pretty"
curl -X GET "10.75.12.165:9900/knowledge_base/back/20974?pretty"
curl -X GET "10.75.12.165:9900/knowledge_base/back/20277?pretty"
符合条件查询,实现类似mysql的and 和in的操作
curl -X GET "10.75.12.165:9900/faq/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": {
"includes": ["id", "title", "content", "keywords","status","location"],
"excludes": ["utime"]
},
"query": {
"bool": {
"should": {
"multi_match": {
"query": "媳妇啊",
"fields": ["keywords^8", "title^4", "content^1"]
}
},
"minimum_should_match":1,
"must":[
{"term":{"id":13487}},
{"term":{"location":1}},
{"bool":{
"should":[
{"term":{"status":1}},
{"term":{"status":2}},
{"term":{"status":3}}
]}}
]
}
},
"highlight": {
"pre_tags": ["", ""],
"post_tags": ["", ""],
"fields": {
"title": {},
"keywords": {},
"content": {}
}
},
"from": 0,
"size": 10
}'