Elasticsearch深入搜索与查询语言介绍DSL

2021-03-01  本文已影响0人  奔跑的程序媛A

Elasticsearch两种查询

  1. url 搜索 e.g. GET twitter/_search?q=user:kimchy
  2. DSL查询语言
    GET /_search
    {
    "query": { 
        "bool": { 
        "must": [
            { "match": { "title":   "Search"        }}, 
            { "match": { "content": "Elasticsearch" }}  
        ],
        "filter": [ 
            { "term":  { "status": "published" }}, 
            { "range": { "publish_date": { "gte": "2015-01-01" }}} 
        ]
        }
    }
    }

DSL(Domain Specific Language)

基本语法

GET /_search
{
    "query": <query clause>
}

查询分类

查询语法

1. 全文查询


GET /_search
{
    "query" : {
        "match" : {
            "full_name" : "John Smith"
        },
        "multi_match": {
          "query": "John Smith",
          "fields": ["first_name", "full_name"]
        }
    }
}

2. Term-level 查询

Term-level 查询是直接查询倒排索引中的确切的值。
Term-level 查询通常用于结构化的数据,如数值、日期、枚举值或关键字,而不是文本(text)

POST /_search
{
  "query": {
    "term" : { "first_name" : "john" } ,
    "terms" : { "full_name" : ["john", "john2"] },
    "range" : {
            "info.age" : {
                "gte" : 28,
                "lt" : 60,
                "boost" : 2.0
            }
        },
    "wildcard" : { "full_name" : "john*" },
    "regexp" : { "full_name" : "jo.*" },
     "type" : {
            "value" : "user"
        }

  }
}

3. 复合查询


GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "full_name" : "john"}
            },
            "boost" : 1.5
        }
    }
}

POST /_search
{
    "query": {
        "bool" : {
            "must" : {
              "term" : { "last_name" : "smith" }
            },
            "filter": {
              "term" : { "info.interests" : "musics" }
            },
            "must_not" : {
              "range" : {
                "info.age" : { "gte" : 10, "lte" : 25 }
              }
            },
            "should" : [
              { "term" : { "full_name" : "john" } },
              { "term" : { "full_name" : "smith" } }
            ],
            "minimum_should_match" : 1,
            "boost" : 2.0
        }
    }

参考:https://blog.csdn.net/lamp_yang_3533/article/details/97618687

上一篇 下一篇

猜你喜欢

热点阅读