说明:这些命令针对es7.x
,6 以下版本
可能无效,elastic 6 / 7
不同版本命令差别较大。。
创建索引
这里创建索引名为fc_search
,type
和data
是关键字类型,也就是不进行分词,keyword_1
是text
类型,默认分词,并且指定分词方式为ik_smart
。
curl -u elastic:changeme -X PUT http://192.168.20.130:9200/fc_search -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
},
"mappings" : {
"properties": {
"id": {
"type" : "long"
},
"type": {
"type": "long"
},
"keyword_1": {
"type": "text",
"analyzer" : "ik_max_word"
},
"keyword_2": {
"type": "text",
"analyzer" : "ik_max_word"
},
"keyword_3": {
"type": "text",
"analyzer" : "ik_max_word"
},
"data": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
},
"updated_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
}
}
}
}'
添加文档
curl -u elastic:changeme -X POST http://192.168.20.130:9200/fc_search/_doc/1 -H 'Content-Type: application/json' -d'
{
"id": 10000,
"data": "data",
"keyword_1": "蓝色渐变时尚家居通用PPT模板,弧形,家居,地板,装修,蓝色,曲线,通用,家居,时尚,其他"
}'
说明:http://192.168.20.130:9200/fc_search/_doc/1?pretty
, 这里的_doc表示类型,1表示doc_id 。
查询
-
查询所有
curl -u elastic:changeme -X GET http://192.168.20.130:9200/fc_search/_search
结果:
{"_index":"fc_search","_type":"_doc","_id":"5_1","_version":1,"_seq_no":5,"_primary_term":1,"found":true,"_source":{"id":1,"type":5,"keyword_1":"测试1","keyword_2":"测试2","keyword_3":"测试3"}}
-
根据文档id查询
curl -u elastic:changeme -X GET http://192.168.20.130:9200/fc_search/_doc/5_1
结果不美观,es里面查询如果想让json结果规整,可以末尾加
?pretty
curl -u elastic:changeme -X GET http://192.168.20.130:9200/fc_search/_doc/5_1?pretty
结果:
{ "_index" : "fc_search", "_type" : "_doc", "_id" : "5_1", "_version" : 1, "_seq_no" : 5, "_primary_term" : 1, "found" : true, "_source" : { "id" : 1, "type" : 5, "keyword_1" : "测试1", "keyword_2" : "测试2", "keyword_3" : "测试3" } }
-
根据关键字查询(模糊匹配)
curl -u elastic:changeme -X POST http://192.168.20.130:9200/fc_search/_search?pretty -H 'Content-Type: application/json' -d' { "query": { "match": { "keyword_1.keyword": "蓝色,简约" } } }'
这种方式查询,是模糊匹配,忽略分词。
-
根据
分词
查询curl -u elastic:changeme -X POST http://192.168.20.130:9200/fc_search/_search?pretty -H 'Content-Type: application/json' -d' { "from": 0, "size": 20, "query": { "bool": { "should": [{ "match": { "keyword_1": "蓝色,简约" } } ] } } }'
keyword_1
是被分词了的,这种方式是按照分词匹配,搜索关键词也会被分词为:
修改索引(给字段添加分词属性)
创建索引fc_search_copy
curl -u elastic:changeme -X PUT http://192.168.20.130:9200/fc_search_copy -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
},
"mappings" : {
"properties": {
"id": {
"type" : "long"
},
"type": {
"type": "long"
},
"keyword_1": {
"type": "text",
"analyzer" : "ik_max_word"
},
"keyword_2": {
"type": "text",
"analyzer" : "ik_max_word"
},
"keyword_3": {
"type": "text",
"analyzer" : "ik_max_word"
},
"data": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
},
"updated_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
}
}
}
}'
查看新建立的索引
curl -u elastic:changeme -X GET http://192.168.20.130:9200/fc_search_copy/_mapping
同步数据到新的索引
curl -u elastic:changeme -X POST http://192.168.20.130:9200/_reindex -H 'Content-Type: application/json' -d'
{
"source": {
"index": "fc_search"
},
"dest": {
"index": "fc_search_copy"
}
}'
删除旧的索引
curl -u elastic:changeme -X DELETE http://192.168.20.130:9200/fc_search
新的索引添加别名
curl -u elastic:changeme -X POST http://192.168.20.130:9200/_aliases -H 'Content-Type: application/json' -d'
{
"actions": [
{
"add": {
"index": "fc_search_copy",
"alias": "fc_search"
}
}
]
}'
思路是将旧索引数据复制一份到新的索引,然后删除旧索引,最后将新索引添加别名,别名就是旧索引的名字,这样不会影响原来的代码。
常用命令
-
获取
es
集群信息,包括版本和集群名称等。curl -u elastic:changeme -X GET http://192.168.20.130:9200
获取到的信息如下:
{ "name" : "1653018488005162032", "cluster_name" : "es-lp827qju", "cluster_uuid" : "UuxuP35SQjqInlTs0cNDkA", "version" : { "number" : "7.14.2", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "da023f0226db21490ed0b4bb422b13d8f1926863", "build_date" : "2022-04-14T08:25:18.177087482Z", "build_snapshot" : false, "lucene_version" : "8.9.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
-
查看索引
mapping
即字段属性信息:http://192.168.20.130:9200/fc_search_test/_mapping
结果:
{ "fc_search_test": { "mappings": { "properties": { "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" }, "data": { "type": "keyword" }, "id": { "type": "long" }, "keyword_1": { "type": "text", "analyzer": "ik_max_word" }, "keyword_2": { "type": "text", "analyzer": "ik_max_word" }, "keyword_3": { "type": "text", "analyzer": "ik_max_word" }, "type": { "type": "long" }, "updated_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" } } } } }
-
查询全部数据
http://192.168.20.130:9200/fc_search_test/_search
-
清空索引数据
curl -u elastic:changeme -X POST http://192.168.20.130:9200/ppt_content/_delete_by_query -H 'Content-Type: application/json' -d' { "actions": [ { "query": { "match_all": {} } } ] }'
-
根据文档id查询数据
curl -u elastic:changeme -X GET http://192.168.20.130:9200/fc_search/_doc/5_5?pretty
-
给索引添加别名
curl -u elastic:changeme -X POST http://192.168.20.130:9200/_aliases -H 'Content-Type: application/json' -d' { "actions": [ { "add": { "index": "fc_search_copy", "alias": "fc_search" } } ] }'
查询时候,索引名跟索引别名效果是一样的。
-
删除索引
curl -u elastic:changeme -X DELETE http://192.168.20.130:9200/fc_search_test
文章评论