elasticsearch-5. 查询语言

查询语言

GET /bank/_search
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10,
  "sort": { "balance": { "order": "desc" } },
  "_source": ["account_number", "balance"]
}
  • query : 查询定义
    • match_all : 查询类型,查询某个索引下的所有文档
  • from :指定从哪个文档角标开始返回。默认为0。此例子中返回10-19
  • size :限定从from开始返回的文档数量。如果不指定的话,默认为10个文档
  • sort :排序
    • balance 按照balance字段排序
      • order asc 顺序, desc 倒序
  • _source :指定返回字段
GET /bank/_search
{
  "query": { "match": { "account_number": 20 } }
}
  • match :指定字段查询,此例子返回bank索引中account_number=20的文档
  1. 返回所有address字段中包含mill的文档
GET /bank/_search
{
  "query": { "match": { "address": "mill" } }
}
  1. 返回所有address字段中包含mill或lane的文档
GET /bank/_search
{
  "query": { "match": { "address": "mill lane" } }
}
  1. 返回所有address字段中包含mill lane语句的文档
GET /bank/_search
{
  "query": { "match_phrase": { "address": "mill lane" } }
}
  • match_phrase :匹配语句
  1. bool查询,使用布尔逻辑将小的查询构成大的查询
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

此例子由两个match查询构成,返回address字段中包含mill和lane的文档

  • bool 布尔查询
    • must match返回必须都是true才符合条件
GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

返回address字段中包含mill或lane的文档

  • bool 布尔查询
    • should 指定查询列表,列表中至少有一个条件为true则符合
GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

此例子由两个match查询构成,返回address字段中不包含mill和lane的文档

  • bool 布尔查询
    • must_not match返回必须都是false才符合条件
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

返回 age = 40 同时 state != ID 的文档

你可能感兴趣的:(elasticsearch-5. 查询语言)