如题:
学习 elasticsearch 的时候,需要测试数据,我们可以导入官方提供的文档 accounts.json数据

[elasticsearch 官方测试数据](https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json)

以上地址已经失效了,可以详见can not find accounts.json in the official documentation #88146

新的下载地址 accounts.json 点击下载

可以查看源站

源站文档

先创建索引

POST /bank/customer/
{}

批量导入数据

PUT /bank/customer/_bulk
导入数据

https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json

PUT /bank/customer/_bulk
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}

查询数据

GET /bank/_search
{
  "query": {"match_all": {}}
}

在这里插入图片描述
简单总结es返回的信息:

took – es查询所花费的时间,单位是毫秒
timed_out –请求是否超时
_shards – 查询了多少个分片、成功、失败或者跳过了哪些
max_score – 最大的相关性得分
hits.total.value - 文档匹配到的数量
hits.sort -文档的排序位置(不按相关性得分排序时)
hits._score -文档的关联度得分(使用match_all 时不适用)

查询所有并 account_number 字段升序排序

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

查询的结果默认展示的是前10条数据

范围查询,from 和size

表示查询的数据从 from开始,之后的size个数据

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 10,
  "size": 10
}

query match

To search for specific terms within a field, you can use a match query. For example, the following request searches the address field to find customers whose addresses contain mill or lane:
根据某个具体的字段查询,可以使用match query,以下表示根据查找索引 band 中 字段 address包含值 mill ,lane 的字段值

GET /bank/_search
{
  "query": { "match": { "address": "mill lane" } }
}

query match_phrase

和 query match相比来说,match_phrase ,指定的字段 address,不会进行分词
match,查询字段 address 中的值 “mill lane”,会将字符串转化成 mill 或者 lane
match_phrase 查询字段时候,不会分词,查询的还是 “mill lane”字段

GET /bank/_search
{
  "query": { "match_phrase": { "address": "mill lane" } }
}

bool 复杂的条件查询

To construct more complex queries, you can use a bool query to combine multiple query criteria. You can designate criteria as required (must match), desirable (should match), or undesirable (must not match).

For example, the following request searches the bank index for accounts that belong to customers who are 40 years old, but excludes anyone who lives in Idaho (ID):

构造复杂的查询语句,需要使用 bool 来组合多个查询条件,你可以设计 must matchshould match 已经 must_not

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

filter

这个仅仅影响结果,但是并不影响相关性的得分情况

GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐