Elasticsearch之高亮查詢,聚合查詢


Elasticsearch之高亮查詢

一 前言

如果返回的結果集中很多符合條件的結果,那怎么能一眼就能看到我們想要的那個結果呢?比如下面網站所示的那樣,我們搜索elasticsearch,在結果集中,將所有elasticsearch高亮顯示?

06119F24-7838-43D8-84EE-F20B929C16B7

如上圖我們搜索百度一樣。我們該怎么做呢?

二 准備數據

PUT lqz/doc/4
{
  "name":"石頭",
  "age":29,
  "from":"gu",
  "desc":"粗中有細,狐假虎威",
  "tags":["", "",""]
}

三 默認高亮顯示

我們來查詢:

GET lqz/doc/_search
{
  "query": {
    "match": {
      "name": "石頭"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

#我們使用highlight屬性來實現結果高亮顯示,需要的字段名稱添加到fields內即可,elasticsearch會自動幫我們實現高亮。
結果如下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.5098256,
    "hits" : [
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.5098256,
        "_source" : {
          "name" : "石頭",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有細,狐假虎威",
          "tags" : [
            "",
            "",
            ""
          ]
        },
        "highlight" : {
          "name" : [
            "<em>石</em><em>頭</em>"
          ]
        }
      }
    ]
  }
}
查詢結果

上例中,elasticsearch會自動將檢索結果用標簽包裹起來,用於在頁面中渲染。

四 自定義高亮顯示

GET lqz/chengyuan/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "highlight": {
    "pre_tags": "<b class='key' style='color:red'>",
    "post_tags": "</b>",
    "fields": {
      "from": {}
    }
  }
}
上例中,在highlight中,pre_tags用來實現我們的自定義標簽的前半部分,在這里,我們也可以為自定義的標簽添加屬性和樣式。post_tags實現標簽的后半部分,組成一個完整的標簽。至於標簽中的內容,則還是交給fields來完成。
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "lqz",
        "_type" : "chengyuan",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "老二",
          "age" : 30,
          "sex" : "male",
          "birth" : "1070-10-11",
          "from" : "gu",
          "desc" : "皮膚黑,武器長,性格直",
          "tags" : [
            "",
            "",
            ""
          ]
        },
        "highlight" : {
          "name" : [
            "<b class='key' style='color:red'>老</b><b class='key' style='color:red'>二</b>"
          ]
        }
      }
    ]
  }
}
查詢結果

需要注意的是:自定義標簽中屬性或樣式中的逗號一律用英文狀態的單引號表示,應該與外部elasticsearch語法的雙引號區分開

前后端分離,你怎么處理?把<b class='key' style='color:red'>串直接以json格式返回,前端自行渲染

Elasticsearch之聚合查詢

  • avg

  • max

  • min

  • sum

avg

# 查詢`from`是`gu`的人的平均年齡。
# select max(age) as my_avg from user;

GET lqz/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "_source": ["name", "age"]
}

上例中,首先匹配查詢fromgu的數據。在此基礎上做查詢平均值的操作,這里就用到了聚合函數,其語法被封裝在aggs中,而my_avg則是為查詢結果起個別名,封裝了計算出的平均值。那么,要以什么屬性作為條件呢?是age年齡,查年齡的什么呢?是avg,查平均年齡。

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "石頭",
          "age" : 29
        }
      },
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "顧老二",
          "age" : 30
        }
      },
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "龍套偏房",
          "age" : 22
        }
      }
    ]
  },
  "aggregations" : {
    "my_avg" : {
      "value" : 27.0
    }
  }
}
查詢結果

上例中,在查詢結果的最后是平均值信息,可以看到是27歲。

雖然我們已經使用_source對字段做了過濾,但是還不夠。我不想看都有哪些數據,只想看平均值怎么辦?別忘了size!

GET lqz/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0, 
  "_source": ["name", "age"]
}

上例中,只需要在原來的查詢基礎上,增加一個size就可以了,輸出幾條結果,我們寫上0,就是輸出0條查詢結果。

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_avg" : {
      "value" : 27.0
    }
  }
}
查詢結果

max

GET lqz/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_max": {
      "max": {
        "field": "age"
      }
    }
  },
  "size": 0
}

上例中,只需要在查詢條件中將avg替換成max即可。

min

GET lqz/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_min": {
      "min": {
        "field": "age"
      }
    }
  },
  "size": 0
}

sum

# 求年齡總和
GET lqz/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_sum": { "sum": { "field": "age" } } }, "size": 0 }

分組查詢

現在我想要查詢所有人的年齡段,並且按照15~20,20~25,25~30分組,並且算出每組的平均年齡。

GET lqz/doc/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 15,
            "to": 20
          },
          {
            "from": 20,
            "to": 25
          },
          {
            "from": 25,
            "to": 30
          }
        ]
      },
      "aggs": {
        "my_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}
{
 "took" : 1,
 "timed_out" : false,
 "_shards" : {
   "total" : 5,
   "successful" : 5,
   "skipped" : 0,
   "failed" : 0
 },
 "hits" : {
   "total" : 5,
   "max_score" : 0.0,
   "hits" : [ ]
 },
 "aggregations" : {
   "age_group" : {
     "buckets" : [
       {
         "key" : "15.0-20.0",
         "from" : 15.0,
         "to" : 20.0,
         "doc_count" : 1,
         "my_avg" : {
           "value" : 18.0
         }
       },
       {
         "key" : "20.0-25.0",
         "from" : 20.0,
         "to" : 25.0,
         "doc_count" : 1,
         "my_avg" : {
           "value" : 22.0
         }
       },
       {
         "key" : "25.0-30.0",
         "from" : 25.0,
         "to" : 30.0,
         "doc_count" : 2,
         "my_avg" : {
           "value" : 27.0
         }
       }
     ]
   }
 }
}
查詢結果

上例中,在aggs的自定義別名age_group中,使用range來做分組,field是以age為分組,分組使用ranges來做,fromto是范圍,我們根據需求做出三組。在分組下面,我們使用aggsage做平均數處理,這樣就可以了。返回的結果中可以看到,已經拿到了三個分組。doc_count為該組內有幾條數據,此次共分為三組,查詢出4條內容。還有一條數據的age屬性值是30,不在分組的范圍內!

注意:聚合函數的使用,一定是先查出結果,然后對結果使用聚合函數做處理

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM