基本命令
curl -XGET 'http://localhost:9200/_cat/indices?v'
curl 'localhost:9200/_mapping?pretty=true'
新建和删除Index
curl -XPUT 'localhost:9200/weather'
curl -XDELETE 'localhost:9200/weather'
创建模板
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/account' -d '
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'
向指定的数据操作
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/account/person/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库guanli"
}'
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/account/person' -d '
{
"user": "李四",
"title": "工程师",
"desc": "数据库db"
}'
查看记录
curl 'localhost:9200/accounts/person/1?pretty=true'
curl -XDELETE 'localhost:9200/account/person/1'
curl -H"Content-Type: application/json" -XPUT 'localhost:9200/account/person/1' -d '
{
"user": "李四",
"title": "工程师",
"desc": "数据库管理员"
}'
数据查询
curl 'localhost:9200/account/person/_search'
全文搜索
curl -H"Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
"query": {"match": {"desc": "软件"}}
}'
curl -H"Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
"query": {"match": {"desc": "数据库管理"}},
"size": 1
}'
curl -H "Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
"query": {"match": {"desc": "数据库管理"}},
"from": 0,
"size": 1
}'
curl -H "Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
"query": {"match": {"desc": "数据库 管理"}}
}'
curl -H "Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
"query": {
"bool":{
"must": [
{"match": {"desc": "数据库"}},
{"match": {"desc": "管理"}}
]
}
}
}'
上面检索会自动分词,如果想硬匹配怎么办
{
"query": {
"match_phrase": {
"content" : {
"query" : "我的宝马多少马力"
}
}
}
}
查看IK分词结果
curl -XGET "http://es-cn-v0h0qdjdl000acisw.elasticsearch.aliyuncs.com:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_smart",
"text": "hello world, 万里长城永不倒,千里黄河水滔滔,君不见黄河之水天上来 "
}'
查看索引account下面type:person 的 mapping
curl -XGET -H "Content-Type: application/json" 'localhost:9200/accout/_mapping/person'
参考