ES(三):ES条件查询、排序

首先向ES中插入几条数据:

PUT index3
PUT index3/user1/1
{
  "name":"water.zhou",
  "birthday":"1996-6-6",
  "interest":"swimming climbing walking",
  "address":"ShanDong province",
  "company":"yunzhihui",
  "height":178
}

POST index3/user1
{
  "name":"leo.liu",
  "birthday":"1997-5-1",
  "interest":"boxing computer walking",
  "address":"beijing chaoyangqu",
  "company":"tianji",
  "height":175
}

POST index3/user1
{
  "name":"leo.wang",
  "birthday":"1990-10-1",
  "interest":"computer food",
  "address":"beijing chaoyangqu",
  "company":"yunzhihui",
  "height":170
}

这里我们为第一条数据指定了ID,其他数据使用ES自动分配的ID。

1、简单字段查询和排序

GET index3/user1/_search?q=name:water.zhou  //按name查找

GET index3/user1/_search?q=interest:computer&sort=height:asc    //按interest查找,并按照height升序排序

2、query条件查询

2.1、term、terms、分页查询

term、terms查询, query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date等明确值的

分页: from、size,from  从第几条数据开始展示,size 一页展示多少条数据

//term:查询某个字段里含有某个关键词的文档
GET index3/user1/_search
{
  "query":{
    "term":{"company":"yunzhihui"}
  }
}


//terms:查询某个字段里含有多个关键词的文档
GET index3/user1/_search
{
  "query":{
    "terms":{
      "interest":["computer","walking"]
    }
  }
}


GET index3/user1/_search
{
  "from": 2,  //从第三条开始展示,索引从0开始计
  "size":10,  //展示10条
  "query":{
    "terms":{
      "interest":["computer","walking"]
    }
  }
}

2.2、 match、march_all查询

match, query 知道分词器的存在,会对field进行分词操作,然后再查询,它和term区别可以理解为term是精确查询,这边match模糊查询。match_all用来查询全部。

GET index3/user1/_search
{
  "query":{
    "match": {
      "interest": "computer walking"
    }
  }
}


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

查询结果:

可以看出查询结果中,只要interest字段中包含有computer或walking的数据,都会被查询到。这也说明,match查询时,会将查询条件进行分词。但是这里match查询能指定的查询的条件,只能有一个,如果我们要查询同事满足两个字段如interest和address两个字段,match就无法满足。

2.3、multi_match、match_phrase_prefix  和 match_phrase

multi_match可以指定多个查询字段,同match一样,query会对field进行分词操作,然后再查询;match_phrase_prefix 前缀匹配;match_phrase用于匹配短语查询,属于精确匹配,不会对field进行分词。

GET index3/user1/_search
{
  "query":{
    "multi_match": {
      "query": "shandong computer",
      "fields": ["interest","address"]
    }
  }
}


GET index3/user1/_search
{
  "query": {
    "match_phrase": {
      "address": "beijing chaoyangqu"
    }
  }
}

GET index3/user1/_search
{
  "query":{
    "match_phrase_prefix": {
      "address": "beijing"   //匹配address前缀为beijing的数据
    }
  }
}

查询的结果分别为:

2.4、_source: 指定查询结果要展示的字段;sort:排序,指定排序字段和排序方式

GET index3/user1/_search
{
  "_source":["name","interest"],  //查询结果只展示name和interest字段
  "query":{
    "multi_match": {
      "query": "shandong computer",
      "fields": ["interest","address"]
    }
  }
}

// 或者可以使用下面这种方式
GET index3/user1/_search
{
  "_source":{
    "includes":["name","interest"],  // 包含哪些字段
    "excludes":["company"] //排除哪些字段,优先级高于includes
  }, 
  "query":{
    "multi_match": {
      "query": "shandong computer",
      "fields": ["interest","address"]
    }
  }
}

//排序
GET index3/user1/_search
{
  "_source":{
    "includes":["name","interest"],
    "excludes":["company"]
  }, 
  "query":{
    "multi_match": {
      "query": "shandong computer",
      "fields": ["interest","address"]
    }
  }, 
 "sort": [  //排序字段类型只能选数字类型,简写 "sort":[{"height":"desc"}]
    {
      "height": {
        "order": "desc"
      }
    }
  ]
}

2.5、范围查询,range

range用于查询某个字段值的范围,常用的属性有:from、to、gt、gte、lt、lte、include_lower、include_upper、boost(设置权重)

GET index3/user1/_search
{
  "query": {
    "range": {
      "height": {
        "gte": 170,
        "lte": 178
      }
    }
  }
}

2.6、通配符查询 wildcard

wildcard可以使用通配符查询,“ * ”和“ ? ”,同SQL中一样,“ * ” 代表任意多个字符, “ ? ”代表任意一个字符  

GET index3/user1/_search
{
  "query": {
    "wildcard": {
      "name": "*zhou"
    }
  }
}

2.7、模糊查询 fuzzy,也叫容错查询

ES中fuzzy模糊查询与sql中的模糊查询有所区别,这里的模糊查询是指我们只查某个字段的相似值,也可以查询出想要的数据。

GET index3/user1/_search
{
  "query": {
    "fuzzy": {
      "name":"wate.zhou"   //会查出来name = water.zhou 的数据
    }
  }
}

// 常用属性
GET index3/user1/_search
{
  "query": {
    "fuzzy": {
      "name":{
        "value":"wate.zhou",
        "boost": 1,       // 权重值,默认是1
        "prefix_length":"4"  // 指明区分分词项的共同前缀长度,默认是0
      }
    }
  }
}

2.8、高亮搜索, hightlight 指定高亮的字段

GET index3/user1/_search
{
  "query":{
    "match": {
      "interest": "computer,walking"
    }
  },
  "highlight": {
     "fields": {
        "interest": {}     //这里只能高亮match查询的字段
      }
  }
}

3、bool、Filter过滤查询

3.1、bool组合查询

bool用于实现组合过滤查询,bool中常用属性有must、should、must_not、filter

  • must: 必须匹配,相当于slql中的  and,计算相关度
  • should: 至少满足一条,相当于slql中的  or,计算相关度
  • must_not:必须不匹配,相当于slql中的  not,不计算相关度
  • filter: 过滤子句,必须匹配,不计算相关度

使用格式{"bool":{"must":[],"should":[],"must_not":[]}}

3.2、filter过滤查询

filter 与 query 对比:

  • filter,不需要计算相关度分数,不需要按照相关度分数进行排序,同时还有内置的自动cache最常使用filter的数据, 性能好
  • query,要计算相关度分数,按照分数进行排序,而且无法cache结果.
GET index3/user1/_search
{
  "query": {
    "bool": {
      "filter":{
        "match":{
            "name": "leo.liu"
            }
          }
      }
  } 
}


GET index3/user1/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {"name": "leo.wang"}}, 
        {"match": {"name": "water.zhou"}}
      ],
      "must": [
        {"term": {"birthday": "1996-6-6"}}
      ],
      "must_not": [
        {"term": {"address": "ShanDong province"}} 
      ]
    }
  }
}

3.3、字段非空过滤:exists

exists用于过滤非空字段,相当于sql中的  not null  查询。

GET index3/user1/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "name"
        }
      }
    }
  }
}
发布了74 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zhoushimiao1990/article/details/104052376