查询数据

查询所有

match_all表示查询所有数据,sort表示按照什么字段来排序

1
2
3
4
5
6
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}

结果:

相关字段解释

  • took – Elasticsearch运行查询所花费的时间(以毫秒为单位)
  • timed_out –搜索请求是否超时
  • _shards - 搜索了多少个碎片,以及成功,失败或跳过了多少个碎片的细目分类。
  • max_score – 找到的最相关文档的分数
  • hits.total.value - 找到了多少个匹配的文档
  • hits.sort - 文档的排序位置(不按相关性得分排序时)
  • hits._score - 文档的相关性得分(使用match_all时不适用)

分页查询(from+size)

from从第几条开始查询,size查询条数

1
2
3
4
5
6
7
8
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
],
"from": 10,
"size": 10
}

查询指定字段(match)

match可在字段中搜索特定字词,如下,则是查询address中包含mill或者lane的数据

1
2
3
{
"query": { "match": { "address": "mill lane" } }
}

查询段落匹配(match_phrase)

match_phrase完全匹配

1
2
3
{
"query": { "match_phrase": { "address": "mill lane" } }
}

多条件查询(bool)

利用bool可以构造更复杂的查询,组合多个查询条件
例如,搜索age为40,且state不为ID的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}