zoukankan      html  css  js  c++  java
  • ES 数据搜索(1)

    1.准备数据:JSON生成器:www.json-generator.com/

    下载官网原数据:https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json

    curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/account/_bulk?pretty&refresh" --data-binary "@accounts.json"

    2.两种search请求方式:

    REST Request URI

             q=*匹配所有文档

             sort 按account_number字段排序 升序

    The pretty parameter, again, just tells Elasticsearch to return pretty-printed JSON results.

    curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty'

      • took - Elasticsearch执行搜索的时间(以毫秒为单位)
      • timed_out - 告诉我们搜索是否超时
      • _shards - 告诉我们搜索了多少碎片,以及搜索碎片成功/失败的次数
      • hits - 搜索结果
      • hits.total - 符合我们搜索条件的文件总数
      • hits.hits - 实际的搜索结果数组(默认为前10个文档)
      • hits.sort - 对结果进行排序键(按分数排序时丢失)
      • hits._score、max_score得分
    {
      "took" : 63,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1000,
        "max_score" : null,
        "hits" : [ {
          "_index" : "bank",
          "_type" : "account",
          "_id" : "0",
          "sort": [0],
          "_score" : null,
          "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
        }, {
          "_index" : "bank",
          "_type" : "account",
          "_id" : "1",
          "sort": [1],
          "_score" : null,
          "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
        }, ...
        ]
      }
    }

    REST Request body    

    同上的rest请求体方式:

    query:请求全部

    sort:按account_number升序排序

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "sort": [
    { "account_number": "asc" }
    ]
    }
    '

     

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} }
    }
    '

     只写from

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "size": 1
    }
    '

     from+size  

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "from": 10,
    "size": 10
    }
    '

     sort  

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "sort": { "balance": { "order": "desc" } }
    }
    '

    只返回关注的字段:account_number,balance

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "_source": ["account_number", "balance"]
    }
    '

     match query:匹配查询 返回account_num=20的结果

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match": { "account_number": 20 } }
    }
    '

     匹配查询 返回address包含mill or  lane的结果  

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match": { "address": "mill lane" } }
    }
    '

    匹配:address包含:mill and lane

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
    {
    "query": { "match_phrase": { "address": "mill lane" } }
    }
    '

     bool查询:must 都匹配  a and b

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'

    {
      "query": {
        "bool": {
          "must": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }


    }
    '

     bool查询:should 可以匹配:a or b  

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'

    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }


    '

     bool查询:must_not 都不匹配

     

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'

    {
      "query": {
        "bool": {
          "must_not": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }


    '

     bool查询:混合查询

    curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'

    {
      "query": {
        "bool": {
          "must": [
            { "match": { "age": "40" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      }
    }



    '

     filter查询:落入filter范围的文档全部等价,不存在谁的score比谁的高  
    GET / bank / _search 
    { 
      “query”:{ bool”:{ 
          “must”:{“match_all”:{}},filter”:{ range”:{ 
              “balance”:{ 
                “gte” 
                “lte”:30000 
              } 
            } 
          } 
        } 
      } 
    }
       


  • 相关阅读:
    FineReport实现java报表报表展示的效果图
    Android开发最佳学习路线图
    void及void指针含义的深刻解析
    7个最好的免费杀毒软件下载
    linux-socket
    java实现第四届蓝桥杯逆波兰表达式
    java实现第四届蓝桥杯逆波兰表达式
    java实现第四届蓝桥杯逆波兰表达式
    java实现第四届蓝桥杯逆波兰表达式
    java实现第四届蓝桥杯有理数类
  • 原文地址:https://www.cnblogs.com/zhxdxf/p/8312781.html
Copyright © 2011-2022 走看看