elasticsearch查询

1、REST Request URI

2、REST Request Body
1)查询设置
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '{
  2. query: {match_all: {}},
  3. size: 5,
  4. from: 0,
  5. sort: {balance: {order: "desc"}},
  6. _source: ["balance", "account_number"]
  7. '
query:设置查询条件
size:返回的数据量
from:数据从哪条开始,第一条是0
sort:排序
_source:返回哪些字段

2)查询条件
  • match query
match_all:所有数据
match:等于或包含某些值的
match_phrase:等于或包含某个值

查询所有数据
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '{query: {match_all: {}}}'
查询account_number(数值类型)是20的数据
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {match: {account_number: 20}}
  4. }'

查询address包含mill的数据
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {match: {address: "mill"}}
  4. }'

查询address包含mill或lane的数据
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {match: {address: "mill lane"}}
  4. }'

查询address包含"mill lane"的数据,文本作为一个整体进行比较
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {match_phrase: {address: "mill lane"}}
  4. }'

  • boolean query

must:相当于and操作
should:相当于or操作
must_not:相当于非操作

address包含mill并且包含lane
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {bool: {must: [{match: {address: "mill"}}, {match: {address: "lane"}}]}}
  4. }'

address包含mill或包含lane
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {bool: {should: [{match: {address: "mill"}}, {match: {address: "lane"}}]}}
  4. }'

address不包含mill并且不包含lane
  1. curl -XPOST http://vm1:9200/customer/external/_search?pretty -d '
  2. {
  3. query: {bool: {must_not: [{match: {address: "mill"}}, {match: {address: "lane"}}]}}
  4. }'

多个条件组合
  1. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "must": [
  6.         { "match": { "age": "40" } }
  7.       ],
  8.       "must_not": [
  9.         { "match": { "state": "ID" } }
  10.       ]
  11.     }
  12.   }
  13. }'
filter query

aggregation query
























原文地址:https://www.cnblogs.com/lishouguang/p/4560935.html