zoukankan      html  css  js  c++  java
  • Elastic Search Nest研究(三)--DSL高级查询

    DSL高级查询

    DSL(domain specific language)查询,是es提出的基于json的搜索方式,在搜素时传入特定的json格式数据完成不同需求的搜索。

    DSL搜索比url搜索更强大。

    1. 查询全部

    GET请求http://localhost:9200/news/_doc/_search

    {
    "query":{
    "match_all":{}
    }
    }

     

    2. 分页查询

    from :是起始文档索引

    size:查询的文档数量

    GET请求http://localhost:9200/news/_doc/_search

    {
    "from":0,
    "size":1,
    "query":{
    "match_all":{}
    }
    }

     

    3.Term查询

    Term查询时不会对关键词分词,对关键词进行整体匹配

    GET请求http://localhost:9200/news/_doc/_search

    查询title中包含开发的记录

    {
        "query": {
          "term": {
          "title":"开发"
          }
      }
    }

    4.根据id查询

    GET请求http://localhost:9200/news/_doc/_search

    查询id为1和2的 记录

    {
      "query": {
          "ids": {
              "values": [
                  "1",
                  "2"
              ]
          }
      }
    }

    5.全文检索

    GET请求http://localhost:9200/news/_doc/_search

    全文检索在对关键词进行分词后,在拿着词条对索引的数据进行查询。

    和term查询的区别就是是否对查询关键字进行分词。

    operator:or 表示 只要有一个词在文档中出现则就符合条件,and表示每个词都在文档中出现则才符合条件。

     

    {
    "query":{
    "match":{
    "content":{
    "query":"bootstrap开发框架",
    "operator":"or"
    }
    }
    }
    }

    以上的业务场景是这样的

    1) 例如搜素关键字为bootstrap开发框架分词为bootstrap开发,框架

    2)再使用分词后的关键字到索引中搜索

    3)设置operator为or,只要一个词匹配成功就返回该记录

    4)使用minimum_should_match可以指定文档匹配词的占比,如

    { "query": { "match" : { "description" : { "query" : "spring开发框架", "minimum_should_match": "80%" } } } }

    设置"minimum_should_match": "80%"表示,三个词在文档的匹配占比为80%,即3*0.8=2.4,向上取整得2,表 示至少有两个词在文档中要匹配成功。

    6.Multi Query

    Multi Query其实一次性可以从多个字段中查询匹配,而之前说的term query和match query都只能匹配一个字段

    GET请求http://localhost:9200/news/_doc/_search

    可以从title和content两个字段中进行搜索,只要一个字段查询满足就可以

    {
    "query":{
    "multi_match":{
    "query":"bootstrap开发",
    "fields":["title","content"],
    "minimum_should_match":"50%"
    }
    }
    }

    • 提升boost

      匹配多个字段时可以提升字段的boost(权重)来提高得分

      { "query": { "multi_match" : { "query" : "spring框架", "minimum_should_match": "50%", "fields": [ "name^10", "description" ] }} }

      name^10” 表示权重提升10倍,执行上边的查询,发现name中包括spring关键字的文档排在前边。

    7.bool查询

    bool查询实现将多个查询组合起来

    三个参数:

    must:文档必须匹配must所包括的查询条件,相当于 “AND”

    should:文档应该匹配should所包括的查询条件其中的一个或多个,相当于 "OR"

    must_not:文档不能匹配must_not所包括的该查询条件,相当于“NOT“

    GET请求http://localhost:9200/news/_doc/_search

    同时满足multi query和term query

    {
      "query": {
          "bool": {
              "must": [
                  {
                      "multi_match": {
                          "query": "bootstrap开发",
                          "fields": [
                              "title^10",
                              "content"
                          ]
                      }
                  },
                  {
                      "term": {
                          "author": "lucy"
                      }
                  }
              ]
          }
      }
    }

    8.过滤和排序

    过滤一般和查询共同使用,过滤的速度要比查询的速度快

    可以添加一个或多个排序,text字段不允许排序

    GET请求http://localhost:9200/news/_doc/_search

    从查询结果中筛选出author是lucy的,并且按照create倒序排列

    {
      "query": {
          "bool": {
              "must": [
                  {
                      "multi_match": {
                          "query": "bootstrap开发",
                          "fields": [
                              "title^10",
                              "content"
                          ]
                      }
                  }
              ],
              "filter": [
                  {
                      "term": {
                          "author": "lucy"
                      }
                  }
              ]
          }
      },
      "sort": [
          {
              "createTime": "desc"
          }
      ]
    }

     

    9.高亮显示

    高亮显示可以将搜索结果一个或多个字突出显示,以便向用户展示匹配关键字的位置。

    GET请求http://localhost:9200/news/_doc/_search

    对title和content中包含关键字的地方进行高亮显示

    {
      "query": {
          "bool": {
              "must": [
                  {
                      "multi_match": {
                          "query": "bootstrap开发",
                          "fields": [
                              "title^10",
                              "content"
                          ]
                      }
                  }
              ],
              "filter": [
                  {
                      "term": {
                          "author": "lucy"
                      }
                  }
              ]
          }
      },
      "sort": [
          {
              "createTime": "desc"
          }
      ],
      "highlight":{
      "pre_tags":"<span style='color:red'>",
      "post_tags":"</span>",
      "fields":{
      "title":{},
      "content":{}
      }
      }
    }

     

  • 相关阅读:
    【字符串哈希】The 16th UESTC Programming Contest Preliminary F
    【推导】The 16th UESTC Programming Contest Preliminary L
    【推导】zoj3846 GCD Reduce
    【spfa】【动态规划】zoj3847 Collect Chars
    【搜索】【组合数学】zoj3841 Card
    【贪心】【字典树】Gym
    【贪心】【后缀自动机】Gym
    【拉格朗日插值法】【找规律】【高精度】Gym
    【二分】【动态规划】Gym
    【软件开发综合实验】文本压缩软件
  • 原文地址:https://www.cnblogs.com/lucyliang/p/14298945.html
Copyright © 2011-2022 走看看