elasticsearch的3个基本问题之读取数据

如何从ES读取数据,包括两种方式,直接获取和通过搜索获取


1. 直接获取

GET index/type/ID的方式可以直接获取到某个index下的某个type的,文档ID是ID的doc。e.g.


curl -XGET http://10.19.26.43:9200/nats-log-2016-04-20/logs/AVQvYyK6aK8LxcWQ324f

and the return

 {"_index":"nats-log-2016-04-20”,"_type":"logs","_id":"AVQvYyK6aK8LxcWQ324f”,

"_version":1,"found":true,

"_source”:{xxx}

}


2. 通过search获取数据


(1) 简单的字段匹配搜索 

GET /megacorp/employee/_search?q=last_name:Smith

e.g.

curl -XGET http://10.19.26.43:9200/nats-log-2016-04-20/logs/_search?q=HostName:search-extng1-cnc0.hlg01


{"took":7450,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits”:{xxx}}


curl -XGET http://10.19.26.43:9200/nats-log-2016-04-20/logs/_search?q=search-extng1-cnc0.hlg01

{"took":9114,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":172019921,"max_score":0.8222273,"hits”:xx}}


(2)单字段的精准匹配

GET /megacorp/employee/_search?q=last_name:"Smith"


(3)使用DSL进行复杂的搜索


1) 字段匹配

GET /index/type/_search

{

    "query" : {

        "filtered" : {

            "filter" : {

                "range" : {

                    "age" : { "gt" : 30 } <1>

                }

            },

            "query" : {

                "match" : {

                    "last_name" : "Smith" <2>

                }

            }

        }

    }

}

2)段落匹配

GET /megacorp/employee/_search

{

    "query" : {

        "match_phrase" : {

            "about" : "rock climbing"

        }

    }

}

3)高亮匹配的关键词

GET /megacorp/employee/_search

{

    "query" : {

        "match_phrase" : {

            "about" : "rock climbing"

        }

    },

    "highlight": {

        "fields" : {

            "about" : {}

        }

    }

}

当我们运行这个查询后,相同的命中结果会被返回,但是我们会得到一个新的名叫 highlight 的部分。在这里包含了 about 字段中的匹配单词,并且会被 <em></em> HTML字符包裹住:

{

   ...

   "hits": {

      "total":      1,

      "max_score"0.23013961,

      "hits": [

         {

            ...

            "_score":         0.23013961,

            "_source": {

               "first_name""John",

               "last_name":   "Smith",

               "age":         25,

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

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

            },

            "highlight": {

               "about": [

                  "I love to go <em>rock</em> <em>climbing</em>" <1>

               ]

            }

         }

      ]

   }

}

你可能感兴趣的:(elasticsearch)