Elasticsearch语法
<pre>
curl 'localhost:9200/_cat/health?v'
curl 'localhost:9200/_cat/nodes?v'
</pre>
查看index
<pre>
curl 'localhost:9200/_cat/indices?v'
</pre>
创建index,名字为customer
<pre>
curl -XPUT 'localhost:9200/customer?pretty'
</pre>
创建记录在type
type为external,index为customer,注意"customer/external/1"的含义
<pre>
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
</pre>
查询刚才的记录
<pre>
curl -XGET 'localhost:9200/customer/external/1?pretty'
</pre>
修改记录
<pre>
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d
'{
"doc": { "name": "Jane Doe", "age": 20 }
}'
</pre>
删除index
删除名字为customer的index,一定要注意REST风格的含义
<pre>
curl -XDELETE 'localhost:9200/customer?pretty'
</pre>
删除customer下面external中id为1的记录
<pre>
curl -XPUT 'localhost:9200/customer/external/1'
</pre>
总结:
<pre>
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
</pre>
如果是创建,ID不是必须的
<pre>
search api
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size":2
}'
</pre>
limit:
"size": 1
from
"from": 10
排序
"sort": { "balance": { "order": "desc" } }
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } },
"size": 1
}'
只返回感兴趣的结构
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'
</pre>
match_all的匹配
结构数字匹配
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}'
</pre>
返回address包含"mill",注意是包含mill
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill" } }
}'
</pre>
address包含"mill"或者"lane"
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill lane" } }
}'
</pre>
match_phrase使用
返回address包含"mill lane",注意是包含mill
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}
</pre>
多条件查询
-
同时满足多个条件,must
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
</pre> -
至少满足其中一个条件,should
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
</pre>