美文网首页
ES7 Data searching

ES7 Data searching

作者: 逸章 | 来源:发表于2020-05-04 23:11 被阅读0次

    The searches that are performed on Elasticsearch may need access to various shards simultaneously, and this will affect the CPU and memory. Users can limit the number of shards that can be accessed at once through the max_concurrent_shard_requests setting.

    一、query_string Query

    类似 URI Query – 把查询条件放在 POST 里面

    准备数据:

    PUT /users/_doc/3
      {
        "name" : "Li Sunke",
        "about": "php,elasticsearch,redis,nginx,swoole"
      }
    
     PUT /users/_doc/4
      {
        "name" : "Qu Sunke",
        "about": "mysql,php"
      }
    

    测试query-string(本例使用AND)查询如下:

    POST /users/_search
      {
        "query": {
          "query_string": {
            "query": "Li AND Sunke",
            "default_field": "name"
          }
        }
      }
    
    图片.png

    测试query-string(本例使用OR)查询如下:

    POST /users/_search
      {
        "query": {
          "query_string": {
            "query": "Li OR Sunke",
            "default_field": "name"
          }
        }
      }
    
    图片.png

    下面来介绍它的参数:

    • query: 需要查询的具体内容
    • default_field: 查询的字段
      默认是_all,即对所有字段进行查询。
      支持多字段——"fields" : ["age", "name"],fields中只要有一个字段满足query的条件即可匹配查询。
      支持一些简单的wildcard写法。比如fields:[“nam*”]即任何nam开头的字段
    • default_operator:默认运算符
      有AND、OR,默认为OR。比如query里面的内容是”cat dog”,两个短语以空格分开,如果default_operator参数为OR,那么只要字段内包含cat或者dog之一就可以匹配。
      如果default_operator为AND,字段内必须同时包含cat和dog才可以匹配。与bool查询挺像的。

    二、正文

    1. 数据准备

    先插入要使用的数据:

    POST facebook/_doc?routing=cactus_flower
    {
      "user": "cactus_flower",
      "postDate": "2017-05-19T13:10:02",
      "message": "Just looking for the user Cactus Flower"
    }
    
    图片.png

    2. Search API

    The search API performs a search using a query string, a parameter, or a request body as the search criteria, and then it returns exact matches.

    接下来我们分别看看这集中形式

    2.1 URI search(即体现query string)

    A URI search can be executed using a URI in the request parameters. This is used for a quick curl test using a simple search, but not all the search options are exposed:

    下面是multi-index syntax示例,这里的query string是指 q:参数.:

    图片.png

    2.2 Request body search

    Search requests are executed with a search DSL. The following example shows the Query DSL within the request body

    搜索:

    GET /facebook/_search
    {
      "query": {
        "term": {
          "user": "cactus_flower"
        }
      }
    }
    

    或者用:

    GET /_search
    {
      "query": {
        "term": {
          "user": "cactus_flower"
        }
      }
    }
    
    图片.png

    Query的说明:

    The query element defines a query in the request body using Query DSL

    3. Sort

    准备数据:

    PUT zhifou/_doc/1
    {
      "name":"顾老二",
      "age":30,
      "from": "gu",
      "desc": "皮肤黑、武器长、性格直",
      "tags": ["黑", "长", "直"]
    }
    
    PUT zhifou/_doc/2
    {
      "name":"大娘子",
      "age":18,
      "from":"sheng",
      "desc":"肤白貌美,娇憨可爱",
      "tags":["白", "富","美"]
    }
    
    PUT zhifou/_doc/3
    {
      "name":"龙套偏房",
      "age":22,
      "from":"gu",
      "desc":"mmp,没怎么看,不知道怎么形容",
      "tags":["造数据", "真","难"]
    }
    
    
    PUT zhifou/_doc/4
    {
      "name":"石头",
      "age":29,
      "from":"gu",
      "desc":"粗中有细,狐假虎威",
      "tags":["粗", "大","猛"]
    }
    
    PUT zhifou/_doc/5
    {
      "name":"魏行首",
      "age":25,
      "from":"广云台",
      "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
      "tags":["闭月","羞花"]
    }
    

    测试查询,比如我们查询顾府及广云台都有哪些人,并根据age字段按照降序:

    GET zhifou/_doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ]
    }
    
    图片.png
    再改一下排序规则: 图片.png

    The mode option controls the array value to be chosen for the sorting document

    图片.png

    4. Source filtering

    我们可以类比mysql的select特定字段,而不是"select * "

    例如: 图片.png
    又例如: 图片.png
    又例如: 图片.png

    相关文章

      网友评论

          本文标题:ES7 Data searching

          本文链接:https://www.haomeiwen.com/subject/utbughtx.html