数据基础操作
添加索引数据
curl -XPUT 'http://localhost:9200/index/log/1' -d '
{
"user": "wenji",
"postDate": "2016-4-8T10:12:00",
"message": "first one"
}'
查看添加数据
curl -XGET 'http://localhost:9200/index/log/1?pretty=true'
显示结果:
wenji:~ wenji$ curl -XGET 'http://localhost:9200/index/log/1?pretty=true'
{
"_index" : "index",
"_type" : "log",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "wenji",
"postDate" : "2016-4-8T10:12:00",
"message" : "first one"
}
}
搜索数据
curl -XGET 'http://localhost:9200/index/log/_search?q=user:wenji&pretty=true'
json格式搜索
Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。
DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。
全文搜索
match 搜索所有 user 字段中带有 wenji 的都会显示出来,例如也有个user为zhuo wenji,也会被查询出来。
curl -XGET 'http://localhost:9200/index/log/_search?pretty=true' -d '
{
"query" : {
"match" : { "user": "wenji" }
}
}'
短语搜索
match_phrase 可以查询相邻且包含的数据。
curl -XGET 'http://localhost:9200/index/log/_search?pretty=true' -d '
{
"query" : {
"match_phrase" : { "user": "zhuo wenji" }
}
}'
搜索全部
curl -XGET 'http://localhost:9200/index/_search?pretty=true' -d '
{
"query" : {
"matchAll" : {}
}
}'
范围搜索
curl -XGET 'http://localhost:9200/index/_search?pretty=true' -d '
{
"query" : {
"range" : {
"postDate" : { "from" : "2015-4-1T13:00:00", "to" : "2016-11-11T14:00:00" }
}
}
}'
高亮搜索
返回结果中会有一个新的部分叫做highlight,这里包含了来自user字段中的文本,并且用来标识匹配到的单词。
curl -XGET 'http://localhost:9200/index/_search?pretty=true' -d '
{
"query" : {
"match_phrase" : { "user": "zhuo wenji" }
},
"highlight": {
"fields" : {
"user" : {}
}
}
}'
删除数据
curl -XDELETE 'http://localhost:9200/{index}/{type}/{id}'