es教程(二)

作者: 睦月MTK | 来源:发表于2019-11-26 10:15 被阅读0次

@snmutsuki
参考文档

适用于7.4版本!命令运行在powershell下

三、搜索
  • 使用match_all:{}搜索全部文档,并依照account_number进行正向排序,默认只查前十条数据

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
    {
       \"query\" : {
          \"match_all\" : {}
       },
       \"sort\" : [
          {\"account_number\" : \"asc\"}
       ]
    }'
    
  • 结果解析

    • took -- 查询操作的所花的时间(毫秒)
    • timed_out -- 查询是否超时
    • _shards -- 该查询在分片上的执行情况
    • hits.max_score -- 查询出的文档中相关度最高的那个文档的_score
    • hits.hits.sort -- 排序的字段的值,如果没指定,则由文档相关度_score排序
    • hits.total.value -- 符合查询条件的文档的个数
  • 使用from屏蔽前面多少条数据不输出,size控制输出多少个

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
     {
       \"query\" : {
           \"match_all\" : {}
       },
       \"sort\" : [
           {\"account_number\":\"asc\"}
       ],
       \"from\" : 20,
       \"size\" : 10
     }'
    
  • 使用match:{key:value}进行自定义查询,该查询匹配的是词

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
     {
       \"query\" : {
         \"match\" : {
            \"address\" : \"mill lane\"
         }
       }
     }'
    
  • 使用match_phrase:{key:value}进行完全匹配的查询

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
    -H "Content-Type: application/json" `
    -d '
    {
      \"query\" : {
        \"match_phrase\" : {
           \"address\" : \"mill lane\"
        }
      }
    }'
    
  • 使用bool:{must/should/must_not:[{key:value},...],...}来进行多条件的复合查询,must表示一定要匹配的,should表示可以匹配的,must_not表示必定不匹配

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
      -H "Content-Type: application/json" `
     -d '
     {
       \"query\" : {
          \"bool\" : {
             \"must\" : [
                {
                   \"match\" : {
                       \"age\" : 40
                    }
                 }
             ],
             \"must_not\" : [
                {
                    \"match\" : {
                       \"state\" : \"ID\"
                    }
                 }
             ]
          }
       }
     }'
    
    • 使filter作为查询的过滤条件,如下表示选取balance在20000到30000之内的
    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
     {
       \"query\" : {
          \"bool\" : {
             \"filter\" : {
                \"range\" : {
                   \"balance\" : {
                       \"gte\" : 20000 ,
                       \"lte\" : 30000
                   }
                }
             }
          }
       }
     }'
    
四、归并(aggregation)操作
  • 使用aggs来进行一个归并操作(即按照一定条件,由es帮你整合数据并返回你想要的结果),该例将会将文档按照state进行分组,并返回各个组下文档的数量,由于size为0,所以结果中的hits.hits是空的,仅有归并操作的结果。

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
     {
       \"size\" : 0 ,
       \"aggs\" : {
          \"anyName\" : {
              \"terms\" : {
                  \"field\" : \"state.keyword\"
              }
          }
       }
     }'
    
  • 可以嵌套使用aggs来对归并的数据进行再次归并,如上面一步查出了每个州的账户数目,如果这次想继续查出每个州的账户的平均余额,则可以使用aggs的嵌套和avg的平均值计算操作。

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
     {
       \"size\" : 0 ,
       \"aggs\" : {
          \"anyName_terms\" : {
              \"terms\" : {
                  \"field\" : \"state.keyword\"
              },
              \"aggs\" : {
                  \"anyName_avg\" : {
                      \"avg\" : {
                          \"field\" : \"balance\"
                      }
                  }
              }
          }
       }
     }'
    
    //...部分结果
    "buckets" : [
            {
              "key" : "TX",
              "doc_count" : 30,
              "anyName_avg" : {
                "value" : 26073.3
              }
            },
            //...
    ]
    //...
    
  • 你更可以直接在terms操作内使用order来进行基于嵌套归并查询结果的排序(即上述的账户平均值),而不是仅仅只可以对分组数量进行排序。

    curl.exe -X GET "localhost:9200/bank/_search?pretty" `
     -H "Content-Type: application/json" `
     -d '
     {
       \"size\" : 0 ,
       \"aggs\" : {
          \"anyName_terms\" : {
              \"terms\" : {
                  \"field\" : \"state.keyword\" ,
                  \"order\" : {
                      \"anyName_avg\" : \"desc\"
                  }
              },
              \"aggs\" : {
                  \"anyName_avg\" : {
                      \"avg\" : {
                          \"field\" : \"balance\"
                      }
                  }
              }
          }
       }
     }'
    

相关文章

网友评论

    本文标题:es教程(二)

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