ElasticSearch入门--第一章

ElasticSearch入门第一章

注:本文仅针对ElasticSearch权威指南(中文版)中出现的简写命令进行curl的示例复原.仅供作为学习笔记使用.

ES权威指南中文版 

链接地址:

http://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html

http://es.xiaoleilu.com/010_Intro/30_Tutorial_Search.html

1.简单的ES搜索请求

#查询索引数量

curl -XGET '192.168.4.3:9201/_count?pretty' -d '

{

    "query": {

        "match_all": {}

    }

}'

#插入数据

curl -XPUT '127.0.0.1:9201/megacorp/employee/1' -d '

{

    "first_name" : "John",

    "last_name" :  "Smith",

    "age" :        25,

    "about" :      "I love to go rock climbing",

    "interests": [ "sports", "music" ]

}'

curl -XPUT '127.0.0.1:9201/megacorp/employee/2' -d '

{

    "first_name" :  "Jane",

    "last_name" :   "Smith",

    "age" :         32,

    "about" :       "I like to collect rock albums",

    "interests":  [ "music" ]

}'

curl -XPUT '127.0.0.1:9201/megacorp/employee/3' -d '

{

    "first_name" :  "Douglas",

    "last_name" :   "Fir",

    "age" :         35,

    "about":        "I like to build cabinets",

    "interests":  [ "forestry" ]

}'

curl -XPUT '127.0.0.1:9201/megacorp/employee/4' -d '

{

    "first_name" :  "Douglas",

    "last_name" :   "Fir2",

    "age" :         35,

    "about":        "I like to build cabinets",

    "interests":  [ "forestry" ]

}'

curl -XPUT '127.0.0.1:9201/megacorp/employee/5' -d '

{

    "first_name" :  "Douglas",

    "last_name" :   "Fir Smith",

    "age" :         35,

    "about":        "I like to build cabinets",

    "interests":  [ "forestry" ]

}'

#查询数据-检索文档

curl -XGET '127.0.0.1:9201/megacorp/employee/1' 

#删除索引

curl -XDELETE '127.0.0.1:9201/megacorp/'

#删除类型

curl -XDELETE '127.0.0.1:9201/megacorp/employee'

#删除文档

curl -XDELETE '127.0.0.1:9201/megacorp/employee/3'

#简单搜索--默认返回前十个数据

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' 

#按关键字查询--查询制定字符串,精确匹配 q=${fieldName}:${fieldValue}

curl -XGET '127.0.0.1:9201/megacorp/employee/_search?q=last_name:Smith' 

#使用DSL语言进行查询

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' -d '

{

    "query" : {

        "match" : {

            "last_name" : "Smith"

        }

    }

}' 

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' -d '

{

    "query" : {

        "match" : {

            "last_name" : "Fir"

        }

    }

}' 

#使用DSL语言进行查寻并添加区间过滤器过滤结果--首先查找年龄大于30的结果再查找lastname为史密斯的结果

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' -d '

{

    "query" : {

        "filtered" : {

            "filter" : {

                "range" : {

                    "age" : { "gt" : 30 } 

                }

            },

            "query" : {

                "match" : {

                    "last_name" : "smith" 

                }

            }

        }

    }

}' 

#相关性查询即全文搜索,包含短语或某个词就返回,然后进行打分--根据相关性评分查找并进行排序

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' -d '

{

    "query" : {

        "match" : {

            "about" : "rock climbing"

        }

    }

}' 

#精确匹配即短语搜索--确切的匹配若干个单词或短语

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' -d '

{

    "query" : {

        "match_phrase" : {

            "about" : "rock climbing"

        }

    }

}' 

#高亮结果--结果在上一个查询结果上添加highlight字段包含来自about中的文本,并用<em></em>来标识单词

curl -XGET '127.0.0.1:9201/megacorp/employee/_search' -d '

{

    "query" : {

        "match_phrase" : {

            "about" : "rock climbing"

        }

    },

    "highlight": {

        "fields" : {

            "about" : {}

        }

    }

}' 

2.简单的聚合请求

#统计所有职员的兴趣爱好 计数

curl -XGET '127.0.0.1:9201/megacorp/employee/_search?pretty' -d '

{   

"aggs": {

    "all_interests": { 

    "terms": { "field": "interests" }

    }

    }

}'

#统计员工名叫Smith的兴趣爱好

curl -XGET '127.0.0.1:9201/megacorp/employee/_search?pretty' -d '

{

"query": {

    "match": {

      "last_name": "smith"

    }

  },

  "aggs": {

    "all_interests": {

     "terms": {

       "field": "interests"

     }

    }

}

}'

#统计所有员工的平均年龄

curl -XGET '127.0.0.1:9201/megacorp/employee/_search?pretty' -d '

{

"aggs" : {

       "avg_age" : {

           "avg" : { "field" : "age" }

       }

   }

}

}'

#统计每种兴趣下员工的平均年龄--分级聚合

curl -XGET '127.0.0.1:9201/megacorp/employee/_search?pretty' -d '

{

"aggs" : {

        "all_interests" : {

            "terms" : { "field" : "interests" },

            "aggs" : {

                "avg_age" : {

                    "avg" : { "field" : "age" }

                }

            }

        }

    }

}'

#按first_name统计聚合结果

curl -XGET '127.0.0.1:9201/megacorp/employee/_search?pretty' -d '

{

"aggs": {

    "all_interests": {

     "terms": {

       "field": "first_name"

     }

    }

}

}'

转载请注明出处

猜你喜欢

转载自harborchung.iteye.com/blog/2333430