1. 程式人生 > >Elasticsearch---學習記錄(4)

Elasticsearch---學習記錄(4)

15.記錄------查詢體

使用GET進行查詢,可以帶入查詢引數.

curl -XGET http://172.16.150.149:29200/facebook,twitter/_search?pretty -d '
{"from":5,"size":3}
'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
"total" : 4,
"successful" : 4,
"failed" : 0
  },
  "hits" : {
"total" : 8,
"max_score" : 1.0,
"hits" : [ {
  "_index" : "facebook",
  "_type" : "blog",
  "_id" : "AWZ668ZcHFL4sAFl7IMI",
  "_score" : 1.0,
  "_source" : {
"title" : "website",
"text" : "blog is making",
"date" : "2018/1016"
  }
}...

16.記錄------匹配字首解析查詢

給定一個首字母,進行匹配.

-XGET http://172.16.150.149:29200/facebook,twitter/_search?pretty -d '
{"query":{
"match_phrase_prefix":{
"text":"blog i"
}}}'

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
"total" : 4,
"successful" : 4,
"failed" : 0
  },
  "hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [ {
  "_index" : "facebook",
  "_type" : "blog",
  "_id" : "AWZ67I_dHFL4sAFl7IMJ",
  "_score" : 1.0,
  "_source" : {
"title" : "website",
"text" : "blog is making",
"date" : "2018/1016"
  }
...

官方強烈推薦再設定match_phrase_prefix來設定後續匹配的個數.

 curl -XGET http://172.16.150.149:29200/facebook,twitter/_search?pretty -d '
{"query":{
"match_phrase_prefix":{
"text":"blog i"
}}}'

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
"total" : 4,
"successful" : 4,
"failed" : 0
  },
  "hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [ {
  "_index" : "facebook",
  "_type" : "blog",
  "_id" : "AWZ67I_dHFL4sAFl7IMJ",
  "_score" : 1.0,
  "_source" : {
"title" : "website",
"text" : "blog is making",
"date" : "2018/1016"
  }
...

17.記錄------多匹配查詢與_score的進一步瞭解

curl -XGET http://172.16.150.149:29200/facebook,twitter/_search?pretty -d '
{"query":{
"multi_match":{
"query":"blog is ",
"fields":["title","text"]}}}'

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
"total" : 4,
"successful" : 4,
"failed" : 0
  },
  "hits" : {
"total" : 4,
"max_score" : 0.41762865,
"hits" : [ {
  "_index" : "facebook",
  "_type" : "blog",
  "_id" : "AWZ67I_dHFL4sAFl7IMJ",
  "_score" : 0.41762865,
  "_source" : {
"title" : "website",
"text" : "blog is making",
"date" : "2018/1016"
  }
...

_score欄位的進一步瞭解

_score欄位代表著被查詢的文章與查詢的相關性程度,分越高,自然相關度就越高.

查詢結果,往往都按照_score進行降序顯示.

es的評分機制,是建立於lucene的評分基礎之上,並且不限於其評分機制.

lucene評分機制

TF/IDF(詞頻/逆文件頻率)演算法

其計算文件得分需要考慮若干的影響因素

文件權重(document boost)
欄位權重(field boost)
協調因子(coord)
逆文件頻率(inverse document frequency)
長度範數(length norm)
詞頻
查詢函式(query norm)

參考詳見還包括了Lucene預設與實際評分公式