elk之restful api 搜索方式

一共6种:

    

1.   query string serch

2.   query DSL

3.   query filter

4.   full-text search

5.   phrase search

6.   highlight search


一,query stringsearch:

概念:所有的search查询都是在http请求后面跟上query string来构建的。

缺点:对复杂的查询语句很难构建。

1.   GET school3/student2/_search  //查询所有的数据。

返回数据结构名词:

Took:返回数据毫秒数。

timed_ou:是否超时。

_shards:5个分片,相当于数据被拆成了5份,分别在每个分片上,当我们进行搜索时,会去这5个分片上搜索,或者去它们对应的replica上搜索。

Hits.total:返回的document总条数。

Hits.max_score:对于 一个serch的匹配度。

Hits.hits:返回的document的详细数据。

 

{

  "took": 106,

  "timed_out":false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

"hits": {

    "total":2,

    "max_score": 1,

    "hits": [

      {

        "_index":"school3",

        "_type":"student2",

        "_id":"AWLxic8IWUX9wu-fMfJ_",

        "_score": 1,

        "_source": {

          "id":"1",

          "name": "少将",

         "addreess": "金牛区",

          "age": 28,

          "phone":"1321323456",

          "score":99

        }

      },

      {

        "_index":"school3",

        "_type":"student2",

        "_id":"AWLxiNJmWUX9wu-fMfJ-",

        "_score": 1,

        "_source": {

          "id":"1",

          "name":"夏文杰2",

         "addreess": "成都市2",

          "age": 23,

          "phone":"13219068156",

          "score":11

        }

      }

]

  }

}

2.   GETschool3/student2/_search?sort=age:desc

查询所有信息以age字段排序。

3.   GET school3/student2/_search?q=age:25

年龄=25的学生数据。

 

二,query DSL:

查询参数放在http body里面,可以用json格式来创建,能构造复杂的查询语法。

1.   查询所有:

GET school3/student2/_search

  {

   "query": { "match_all":{}}

    }

2.   条件查询:

     name包含夏文杰的学生,并且按照age降序排序。

GET school3/student2/_search

  {

   "query":

     {

       "match":{

         "name":"夏文杰"

       }

     },

     "sort":{

       "age":"desc"

     }

 }

3.分页查询:

先查询全部,再截取想要部分的数据。

和sql的limit一样。

GET school3/student2/_search

  {

   "query":

     {

       "match_all":{}

     },

     "from":2,

     "size": 5

  }

4.只查询某些字段:

    

  GETschool3/student2/_search

只查询name和age字段

  {

   "query":

     {

       "match_all":{}

     },

    "_source": ["name","age"]

  }

 

三,query  filter

       对查询进行过滤:

 

1.   GET school3/student2/_search

查询名字包含夏文杰的数据而且年龄大于23的数据。

bool:表示有多个条件查询。

  {

   "query":{

     "bool":{

       "must":{

          "match":{

            "name":"夏文杰"

          }

         },

         "filter":{

           "range": {

              "age": {

                "gt": 23

              }

           }

         }

        }

     }

  }

 

四,全文检索。

1.   GET school3/student2/_search

匹配包含夏文的或者包含 夏文杰,或者包含夏文 夏文杰的数据,匹配度最高的排在最前面,根据score。因为每次匹配都会列出对应的id,如果去匹配最多的,则会排在前面。

  {

   "query":{

       "match":{

         "name":"夏文 夏文杰 "

       }

     }

  }

 

4.phrase search

必须和输入的字符串包含一摸一样关键字才能被搜索出来。

GET school3/student2/_search

查询必须包含夏文杰3的数据

  {

   "query":{

       "match_phrase":{

         "name":"夏文杰3"

       }

     }

  }

关键词: match_phrase

六,highlight search

GET school3/student2/_search

查询name包含夏文杰的数据,并把索引的数据显示为高亮。

  {

   "query":{

       "match ":{

         "name":"夏文杰"

       }

     },

     "highlight": {

       "fields": {

         "name":{}

       }

     }

  }

 

结果:

{

 "took": 535,

 "timed_out": false,

 "_shards": {

   "total": 5,

   "successful": 5,

   "failed": 0

  },

 "hits": {

   "total": 2,

   "max_score": 2.5893893,

   "hits": [

     {

       "_index": "school3",

       "_type": "student2",

       "_id": "AWLxiNJmWUX9wu-fMfJ-",

       "_score": 2.5893893,

       "_source": {

         "id": "1",

         "name": "夏文杰2",

         "addreess": "成都市2",

         "age": 23,

         "phone": "13219068156",

         "score": 11

       },

       "highlight": {

         "name": [

           "2"

         ]

       }

     },

     {

       "_index": "school3",

       "_type": "student2",

       "_id": "4a630ba2c1224205ad8da457c663ef27",

       "_score": 0.8630463,

       "_source": {

         "id": "4a630ba2c1224205ad8da457c663ef27",

         "name": "夏文杰3",

         "addreess": "成都市2",

         "age": 25,

         "phone": "13032894563",

         "score": 11

       },

       "highlight": {

         "name": [

           "3"

         ]

       }

     }

    ]

  }

}

除了返回的结果,还加了highlight,返回的html格式的数据,显示在网页上就是标红。



你可能感兴趣的:(elk)