ElasticSearch常用DSL查询语句

数据:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "jyong-test2",
        "_type" : "_doc",
        "_id" : "qp6TXXQBgwfV3tn_mL-K",
        "_score" : 1.0,
        "_source" : {
          "name" : "张三",
          "age" : 20
        }
      },
      {
        "_index" : "jyong-test2",
        "_type" : "_doc",
        "_id" : "8J6TXXQBgwfV3tn_wr83",
        "_score" : 1.0,
        "_source" : {
          "name" : "李四",
          "age" : 19
        }
      },
      {
        "_index" : "jyong-test2",
        "_type" : "_doc",
        "_id" : "FJ6TXXQBgwfV3tn_6sCh",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五",
          "age" : 30
        }
      },
      {
        "_index" : "jyong-test2",
        "_type" : "_doc",
        "_id" : "OZ6UXXQBgwfV3tn_FMC8",
        "_score" : 1.0,
        "_source" : {
          "name" : "赵六",
          "age" : 25
        }
      }
    ]
  }
}

DSL Operator:

1.查询总数

--查询索引总数
GET jyong-test2/_count


---查询符合条件的总数
GET jyong-test2/_search
{
  "query": {
    "match": {
      "name.keyword": "李四"
    }
  },
  "track_total_hits":true       -------7.x版本此参数必须要加
}

---查询两个索引的总数
GET jyong-test,jyong-test2/_count

1.查询李四

---POST:
GET jyong-test2/_search
{
  "query": {
    "match": {
      "name.keyword": "李四"
    }
  }
}

---GET
GET jyong-test2/_search?q=name:李四

2.查询张三和李四

---POST
GET jyong-test2/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,         -------两个条件成立1个即可(默认是1)
        "should": [
          {
            "match": {
              "name.keyword": "张三"
            }
          },
          {
          "match": {
            "name.keyword": "李四"
            }
          }
        ]
    }
  }
}

--GET
GET jyong-test2/_search?q=name:"张三","李四"

3.查询年龄20到30的,并且按照年龄降序排序

---POST
POST jyong-test2/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 30
      }
    }
  },"sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}
---GET
GET jyong-test2/_search?q=age[20 TO 30]&sort=age:desc              --------TO 必须大写

4.match模糊查询和term精确查询

---精确查询

POST  jyong-test2/_search
{
  "query": {
    "term": {
      "name.keyword": {
        "value": "王五"
      }
    }
  }
}

--模糊查询(相当于like)

GET jyong-test2/_search
{
  "query": {
    "match": {
      "name.keyword": "王五"
    }
  }
}

5.过滤器

POST jyong-test2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],"filter": {
        "term": {
          "name.keyword": "王五"
        }
      }
    }
  }
}



POST jyong-test2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],"filter": {
        "range": {
          "age": {
            "gte": 20,
            "lte": 30
          }
        }
      }
    }
  }
}

6.只显示指定字段


---显示两个字段
--POST

POST jyong-test2/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name","age"]
}

--GET

GET jyong-test2/_search?_source=name,age


---显示一个字段

--GET 

GET jyong-test2/_search?_source=name

--POST

POST jyong-test2/_search
{
  "query": {
    "match_all": {}
  },
  "_source": "name"
}

RUNING.......................

你可能感兴趣的:(大数据,elasticsearch)