ES的常用查询

Elasticsearch 提供了丰富的查询过滤语句,本文整理了一些常用的查询方法。

ES 有两种查询方式。本文主要介绍基于DSL语法的查询

  • 使用 Search Lite API,它从url中获取参数
  • 使用 Json 作为请求体,使用 ES的DSL语法来进行查询
1. 全文级别查询

match 是一个标准查询,可以查询文本、数字、日期格式的数据。match 查询的一个主要用途是全文检索。ES 5.X 以上的版本默认使用BM25算法进行相似度的计算

GET /_search
{
    "query": {
        "match" : {
            "message" : "hello world"
        }
    }
}

match_phrase 与match查询不同,它是精确匹配

GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : "this is a test"
        }
    }
}

multi_match 允许在做match 查询的基础上查询多个字段

GET /_search
{
    "query":{
         "multi_match": {
            "query":    "full text search",
            "fields":   [ "title", "body" ]
        }
    } 
}
2. 词条级别查询

term 用于精确值的查询。使用boost参数可以提高指定字段的分数。boost的默认值为1。

string类型的数据在ES中可以使用text或者keyword的类型来存储。ES存储text类型的数据时会自动分词,然后建立索引。keyword存储数据时,不会分词,直接建立索引。如果需要对string数据进行精确查询,应该使用keyword的类型来存储数据。

GET /_search
{
    "query":{
        "bool":{
            "should":[
                {
                    "term":{
                        "status":{
                            "value":"urgent",
                            "boost":2
                        }
                    }
                },
                {
                    "term":{
                        "status":"normal"
                    }
                }
            ]
        }
    }
}

terms 可以指定一个字段的多个精确值。

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "terms" : { "user" : ["kimchy", "elasticsearch"]}
            }
        }
    }
}

range 用于需要查询指定范围的内容。range 的常用参数有gte (greater-than or equal to), gt (greater-than) ,lte (less-than or equal to) 和 lt (less-than)。ES 的date类型的数值也可以使用range查询。

GET /_search
{
    "query": {
        "range" : {
            "age" : {
                "gte" : 10,
                "lte" : 20,
                "boost" : 2.0
            }
        }
    }
}

exists 返回在原始字段汇中至少有一个非空值的文档

GET /_search
{
    "query": {
        "exists" : { "field" : "user" }
    }
}

prefix 前缀查询

GET /_search
{
    "query": {
        "prefix": {
            "postcode": "W1"
        }
    }
}
3. 复合查询

bool 查询可以合并多个过滤条件查询的结果。bool 查询可由 must, should, must not, filter 组合完成

  • must 查询的内容必须出现在检索到的文档中,并且会计算文档匹配的相关度
  • filter 查询的内容必须出现在检索到的文档中。与must不同,filter中的查询条件不会参与评分。filter对查询的数据有缓存功能。filter效率会比must高一些,一般,除了需要计算相关度的查询,一般使用filter
  • should 至少有一个查询条件匹配,相当于 or
  • must_mot 多个查询条件的相反匹配,相当于 not
GET /_search
{
    "query":{
        "bool":{
            "must":{
                "term":{
                    "user":"kimchy"
                }
            },
            "filter":{
                "term":{
                    "tag":"tech"
                }
            },
            "must_not":{
                "range":{
                    "age":{
                        "gte":10,
                        "lte":20
                    }
                }
            },
            "should":[
                {
                    "term":{
                        "tag":"wow"
                    }
                },
                {
                    "term":{
                        "tag":"elasticsearch"
                    }
                }
            ]
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章