天天看點

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

0.學習目标

獨立安裝Elasticsearch

會使用Rest的API操作索引

會使用Rest的API查詢資料

會使用Rest的API聚合資料

掌握Spring Data Elasticsearch使用

1.Elasticsearch介紹和安裝

使用者通路我們的首頁,一般都會直接搜尋來尋找自己想要購買的商品。

而商品的數量非常多,而且分類繁雜。如果能正确的顯示出使用者想要的商品,并進行合理的過濾,盡快促成交易,是搜尋系統要研究的核心。

面對這樣複雜的搜尋業務和資料量,使用傳統資料庫搜尋就顯得力不從心,一般我們都會使用全文檢索技術,比如之前大家學習過的Solr。

不過今天,我們要講的是另一個全文檢索技術:Elasticsearch。

1.1.簡介

1.1.1.Elastic

Elastic官網:https://www.elastic.co/cn/

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

Elastic有一條完整的産品線及解決方案:Elasticsearch、Kibana、Logstash等,前面說的三個就是大家常說的ELK技術棧。

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.1.2.Elasticsearch

Elasticsearch官網:https://www.elastic.co/cn/products/elasticsearch

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

如上所述,Elasticsearch具備以下特點:

分布式,無需人工搭建叢集(solr就需要人為配置,使用Zookeeper作為注冊中心)

Restful風格,一切API都遵循Rest原則,容易上手

近實時搜尋,資料更新在Elasticsearch中幾乎是完全同步的。

1.1.3.版本

目前Elasticsearch最新的版本是6.3.1,我們就使用6.3.0

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

需要虛拟機JDK1.8及以上

1.2.安裝和配置

為了模拟真實場景,我們将在linux下安裝Elasticsearch。

1.2.1.建立一個使用者leyou

出于安全考慮,elasticsearch預設不允許以root賬号運作。

建立使用者:

useradd leyou
           

設定密碼:

passwd leyou
           

切換使用者:

su - leyou
           

1.2.2.上傳安裝包,并解壓

我們将安裝包上傳到:/home/leyou目錄

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋
JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

解壓縮:

tar -zxvf elasticsearch-6.2.4.tar.gz
           

我們把目錄重命名:

mv elasticsearch-6.2.4/ elasticsearch
           
JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

進入,檢視目錄結構:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.2.3.修改配置

我們進入config目錄:

cd config

需要修改的配置檔案有兩個:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

jvm.options

Elasticsearch基于Lucene的,而Lucene底層是java實作,是以我們需要配置jvm參數。

編輯jvm.options:

vim jvm.options
           

預設配置如下:

-Xms1g
-Xmx1g
           

記憶體占用太多了,我們調小一些:

-Xms512m
-Xmx512m
           

elasticsearch.yml

vim elasticsearch.yml

修改資料和日志目錄:

path.data: /home/leyou/elasticsearch/data # 資料目錄位置
path.logs: /home/leyou/elasticsearch/logs # 日志目錄位置
           

我們把data和logs目錄修改指向了elasticsearch的安裝目錄。但是這兩個目錄并不存在,是以我們需要建立出來。

進入elasticsearch的根目錄,然後建立:

mkdir data
mkdir logs
           
JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

修改綁定的ip:

network.host: 0.0.0.0 # 綁定到0.0.0.0,允許任何ip來通路

預設隻允許本機通路,修改為0.0.0.0後則可以遠端通路
           

目前我們是做的單機安裝,如果要做叢集,隻需要在這個配置檔案中添加其它節點資訊即可。

elasticsearch.yml的其它可配置資訊:

屬性名 說明

cluster.name 配置elasticsearch的叢集名稱,預設是elasticsearch。建議修改成一個有意義的名稱。 
node.name 節點名,es會預設随機指定一個名字,建議指定一個有意義的名稱,友善管理 
path.conf 設定配置檔案的存儲路徑,tar或zip包安裝預設在es根目錄下的config檔案夾,rpm安裝預設在/etc/ elasticsearch 
path.data 設定索引資料的存儲路徑,預設是es根目錄下的data檔案夾,可以設定多個存儲路徑,用逗号隔開 
path.logs 設定日志檔案的存儲路徑,預設是es根目錄下的logs檔案夾 
path.plugins 設定插件的存放路徑,預設是es根目錄下的plugins檔案夾 
bootstrap.memory_lock 設定為true可以鎖住ES使用的記憶體,避免記憶體進行swap 
network.host 設定bind_host和publish_host,設定為0.0.0.0允許外網通路 
http.port 設定對外服務的http端口,預設為9200。 
transport.tcp.port 叢集結點之間通信端口 
discovery.zen.ping.timeout 設定ES自動發現節點連接配接逾時的時間,預設為3秒,如果網絡延遲高可設定大些 
discovery.zen.minimum_master_nodes 主結點數量的最少值 ,此值的公式為:(master_eligible_nodes / 2) + 1 ,比如:有3個符合要求的主結點,那麼這裡要設定為2 
           

1.3.運作

進入elasticsearch/bin目錄,可以看到下面的執行檔案:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

然後輸入指令:

./elasticsearch
           

發現報錯了,啟動失敗:

1.3.1.錯誤1:核心過低

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

我們使用的是centos6,其linux核心版本為2.6。而Elasticsearch的插件要求至少3.5以上版本。不過沒關系,我們禁用這個插件即可。

修改elasticsearch.yml檔案,在最下面添加如下配置:

bootstrap.system_call_filter: false
           

然後重新開機

1.3.2.錯誤2:檔案權限不足

再次啟動,又出錯了:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋
[1]: max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
           

我們用的是leyou使用者,而不是root,是以檔案權限不足。

首先用root使用者登入。

然後修改配置檔案:

vim /etc/security/limits.conf
           

添加下面的内容:

* soft nofile 65536

* hard nofile 131072

* soft nproc 4096

* hard nproc 4096
           

1.3.3.錯誤3:線程數不夠

剛才報錯中,還有一行:

[1]: max number of threads [1024] for user [leyou] is too low, increase to at least [4096]
           

這是線程數不夠。

繼續修改配置:

vim /etc/security/limits.d/90-nproc.conf 
           

修改下面的内容:

* soft nproc 1024
           

改為:

* soft nproc 4096
           

1.3.4.錯誤4:程序虛拟記憶體

[3]: max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

vm.max_map_count:限制一個程序可以擁有的VMA(虛拟記憶體區域)的數量,繼續修改配置檔案, :

vim /etc/sysctl.conf 
           

添加下面内容:

vm.max_map_count=655360
           

然後執行指令:

sysctl -p
           

1.3.5.重新開機終端視窗

所有錯誤修改完畢,一定要重新開機你的 Xshell終端,否則配置無效。

1.3.6.啟動

再次啟動,終于成功了!

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

可以看到綁定了兩個端口:

9300:叢集節點間通訊接口

9200:用戶端通路接口

我們在浏覽器中通路:http://192.168.56.101:9200

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.4.安裝kibana

1.4.1.什麼是Kibana?

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

Kibana是一個基于Node.js的Elasticsearch索引庫資料統計工具,可以利用Elasticsearch的聚合功能,生成各種圖表,如柱形圖,線狀圖,餅圖等。

而且還提供了操作Elasticsearch索引資料的控制台,并且提供了一定的API提示,非常有利于我們學習Elasticsearch的文法。

1.4.2.安裝

因為Kibana依賴于node,我們的虛拟機沒有安裝node,而window中安裝過。是以我們選擇在window下使用kibana。

最新版本與elasticsearch保持一緻,也是6.3.0

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

解壓到特定目錄即可

1.4.3.配置運作

配置

進入安裝目錄下的config目錄,修改kibana.yml檔案:

修改elasticsearch伺服器的位址:

elasticsearch.url: "http://192.168.56.101:9200"
           

運作

進入安裝目錄下的bin目錄:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

輕按兩下運作:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

發現kibana的監聽端口是5601

我們通路:http://127.0.0.1:5601

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.4.4.控制台

選擇左側的DevTools菜單,即可進入控制台頁面:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

在頁面右側,我們就可以輸入請求,通路Elasticsearch了。

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.5.安裝ik分詞器

Lucene的IK分詞器早在2012年已經沒有維護了,現在我們要使用的是在其基礎上維護更新的版本,并且開發為ElasticSearch的內建插件了,與Elasticsearch一起維護更新,版本也保持一緻,最新版本:6.3.0

1.5.1.安裝

上傳課前資料中的zip包,解壓到Elasticsearch目錄的plugins目錄中:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

使用unzip指令解壓:

unzip elasticsearch-analysis-ik-6.3.0.zip -d ik-analyzer
           

然後重新開機elasticsearch:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.5.2.測試

大家先不管文法,我們先測試一波。

在kibana控制台輸入下面的請求:

POST _analyze
{
  "analyzer": "ik_max_word",
  "text":     "我是中國人"
}
           

運作得到結果:

{
  "tokens": [
    {
      "token": "我",
      "start_offset": 0,
      "end_offset": 1,
      "type": "CN_CHAR",
      "position": 0
    },
    {
      "token": "是",
      "start_offset": 1,
      "end_offset": 2,
      "type": "CN_CHAR",
      "position": 1
    },
    {
      "token": "中國人",
      "start_offset": 2,
      "end_offset": 5,
      "type": "CN_WORD",
      "position": 2
    },
    {
      "token": "中國",
      "start_offset": 2,
      "end_offset": 4,
      "type": "CN_WORD",
      "position": 3
    },
    {
      "token": "國人",
      "start_offset": 3,
      "end_offset": 5,
      "type": "CN_WORD",
      "position": 4
    }
  ]
}
           

1.7.API

Elasticsearch提供了Rest風格的API,即http請求接口,而且也提供了各種語言的用戶端API

1.7.1.Rest風格API

文檔位址:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

1526518410240

1.7.2.用戶端API

Elasticsearch支援的用戶端非常多:https://www.elastic.co/guide/en/elasticsearch/client/index.html

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

點選Java Rest Client後,你會發現又有兩個:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

Low Level Rest Client是低級别封裝,提供一些基礎功能,但更靈活

High Level Rest Client,是在Low Level Rest Client基礎上進行的進階别封裝,功能更豐富和完善,而且API會變的簡單

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

1.7.3.如何學習

建議先學習Rest風格API,了解發起請求的底層實作,請求體格式等。

2.操作索引

2.1.基本概念

Elasticsearch也是基于Lucene的全文檢索庫,本質也是存儲資料,很多概念與MySQL類似的。

對比關系:

索引(indices)--------------------------------Databases 資料庫

? 類型(type)-----------------------------Table 資料表

? 文檔(Document)----------------Row 行

? 字段(Field)-------------------Columns 列 
           

詳細說明:

概念 說明

索引庫(indices) indices是index的複數,代表許多的索引, 
類型(type) 類型是模拟mysql中的table概念,一個索引庫下可以有不同類型的索引,比如商品索引,訂單索引,其資料格式不同。不過這會導緻索引庫混亂,是以未來版本中會移除這個概念 
文檔(document) 存入索引庫原始的資料。比如每一條商品資訊,就是一個文檔 
字段(field) 文檔中的屬性 
映射配置(mappings) 字段的資料類型、屬性、是否索引、是否存儲等特性 

是不是與Lucene和solr中的概念類似。

另外,在SolrCloud中,有一些叢集相關的概念,在Elasticsearch也有類似的:

    索引集(Indices,index的複數):邏輯上的完整索引 
    分片(shard):資料拆分後的各個部分 
    副本(replica):每個分片的複制 
           

要注意的是:Elasticsearch本身就是分布式的,是以即便你隻有一個節點,Elasticsearch預設也會對你的資料進行分片和副本操作,當你向叢集添加新資料時,資料也會在新加入的節點中進行平衡。

2.2.建立索引

2.2.1.文法

Elasticsearch采用Rest風格API,是以其API就是一次http請求,你可以用任何工具發起http請求

建立索引的請求格式:

請求方式:PUT

請求路徑:/索引庫名

請求參數:json格式:

{
  "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 2
    }
}

settings:索引庫的設定 
number_of_shards:分片數量 
number_of_replicas:副本數量 
           

2.2.2.測試

我們先用RestClient來試試

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

響應:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

可以看到索引建立成功了。

2.2.3.使用kibana建立

kibana的控制台,可以對http請求進行簡化,示例:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

相當于是省去了elasticsearch的伺服器位址

而且還有文法提示,非常舒服。

2.3.檢視索引設定

文法

Get請求可以幫我們檢視索引資訊,格式:

GET /索引庫名
           
JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

或者,我們可以使用*來查詢所有索引庫配置:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

2.4.删除索引

删除索引使用DELETE請求

文法

DELETE /索引庫名
           

示例

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

再次檢視heima2:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

當然,我們也可以用HEAD請求,檢視索引是否存在:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

2.5.映射配置

索引有了,接下來肯定是添加資料。但是,在添加資料之前必須定義映射。

什麼是映射?

? 映射是定義文檔的過程,文檔包含哪些字段,這些字段是否儲存,是否索引,是否分詞等

隻有配置清楚,Elasticsearch才會幫我們進行索引庫的建立(不一定)

2.5.1.建立映射字段

文法

請求方式依然是PUT

PUT /索引庫名/_mapping/類型名稱
{
  "properties": {
    "字段名": {
      "type": "類型",
      "index": true,
      "store": true,
      "analyzer": "分詞器"
    }
  }
}
           

類型名稱:就是前面将的type的概念,類似于資料庫中的不同表字段名:任意填寫 ,可以指定許多屬性,例如:

type:類型,可以是text、long、short、date、integer、object等 
index:是否索引,預設為true 
store:是否存儲,預設為false 
analyzer:分詞器,這裡的ik_max_word即使用ik分詞器 
           

示例

發起請求:

PUT heima/_mapping/goods
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "images": {
      "type": "keyword",
      "index": "false"
    },
    "price": {
      "type": "float"
    }
  }
}
           

響應結果:

{

“acknowledged”: true

}

2.5.2.檢視映射關系

文法:

GET /索引庫名/_mapping
           

示例:

GET /heima/_mapping
           

響應:

{
  "heima": {
    "mappings": {
      "goods": {
        "properties": {
          "images": {
            "type": "keyword",
            "index": false
          },
          "price": {
            "type": "float"
          },
          "title": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    }
  }
}
           

2.5.3.字段屬性詳解

2.5.3.1.type

Elasticsearch中支援的資料類型非常豐富:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

我們說幾個關鍵的:

String類型,又分兩種:

text:可分詞,不可參與聚合

keyword:不可分詞,資料會作為完整字段進行比對,可以參與聚合

Numerical:數值類型,分兩類

基本資料類型:long、interger、short、byte、double、float、half_float

浮點數的高精度類型:scaled_float

需要指定一個精度因子,比如10或100。elasticsearch會把真實值乘以這個因子後存儲,取出時再還原。

Date:日期類型

elasticsearch可以對日期格式化為字元串存儲,但是建議我們存儲為毫秒值,存儲為long,節省空間。

2.5.3.2.index

index影響字段的索引情況。

true:字段會被索引,則可以用來進行搜尋。預設值就是true 
false:字段不會被索引,不能用來搜尋 
index的預設值就是true,也就是說你不進行任何配置,所有字段都會被索引。
           

但是有些字段是我們不希望被索引的,比如商品的圖檔資訊,就需要手動設定index為false。

2.5.3.3.store

是否将資料進行額外存儲。

在學習lucene和solr時,我們知道如果一個字段的store設定為false,那麼在文檔清單中就不會有這個字段的值,使用者的搜尋結果中不會顯示出來。

但是在Elasticsearch中,即便store設定為false,也可以搜尋到結果。

原因是Elasticsearch在建立文檔索引時,會将文檔中的原始資料備份,儲存到一個叫做_source的屬性中。而且我們可以通過過濾_source來選擇哪些要顯示,哪些不顯示。

而如果設定store為true,就會在_source以外額外存儲一份資料,多餘,是以一般我們都會将store設定為false,事實上,store的預設值就是false。

2.5.3.4.boost

激勵因子,這個與lucene中一樣

其它的不再一一講解,用的不多,大家參考官方文檔:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

2.6.新增資料

2.6.1.随機生成id

通過POST請求,可以向一個已經存在的索引庫中添加資料。

文法:

POST /索引庫名/類型名
{
    "key":"value"
}
           

示例:

POST /heima/goods/
{
    "title":"小米手機",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2699.00
}
           

響應:

{
  "_index": "heima",
  "_type": "goods",
  "_id": "r9c1KGMBIhaxtY5rlRKv",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 3,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 2
}
           

通過kibana檢視資料:

get _search
{
    "query":{
        "match_all":{}
    }
}

{
  "_index": "heima",
  "_type": "goods",
  "_id": "r9c1KGMBIhaxtY5rlRKv",
  "_version": 1,
  "_score": 1,
  "_source": {
    "title": "小米手機",
    "images": "http://image.leyou.com/12479122.jpg",
    "price": 2699
  }
}

_source:源文檔資訊,所有的資料都在裡面。 
_id:這條文檔的唯一标示,與文檔自己的id字段沒有關聯 
           

2.6.2.自定義id

如果我們想要自己新增的時候指定id,可以這麼做:

POST /索引庫名/類型/id值
{
    ...
}
           

示例:

POST /heima/goods/2
{
    "title":"大米手機",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2899.00
}
           

得到的資料:

{
  "_index": "heima",
  "_type": "goods",
  "_id": "2",
  "_score": 1,
  "_source": {
    "title": "大米手機",
    "images": "http://image.leyou.com/12479122.jpg",
    "price": 2899
  }
}
           

2.6.3.智能判斷

在學習Solr時我們發現,我們在新增資料時,隻能使用提前配置好映射屬性的字段,否則就會報錯。

不過在Elasticsearch中并沒有這樣的規定。

事實上Elasticsearch非常智能,你不需要給索引庫設定任何mapping映射,它也可以根據你輸入的資料來判斷類型,動态添加資料映射。

測試一下:

POST /heima/goods/3
{
    "title":"超米手機",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2899.00,
    "stock": 200,
    "saleable":true
}
           

我們額外添加了stock庫存,和saleable是否上架兩個字段。

來看結果:

{
  "_index": "heima",
  "_type": "goods",
  "_id": "3",
  "_version": 1,
  "_score": 1,
  "_source": {
    "title": "超米手機",
    "images": "http://image.leyou.com/12479122.jpg",
    "price": 2899,
    "stock": 200,
    "saleable": true
  }
}
           

在看下索引庫的映射關系:

{
  "heima": {
    "mappings": {
      "goods": {
        "properties": {
          "images": {
            "type": "keyword",
            "index": false
          },
          "price": {
            "type": "float"
          },
          "saleable": {
            "type": "boolean"
          },
          "stock": {
            "type": "long"
          },
          "title": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    }
  }
}
           

stock和saleable都被成功映射了。

2.7.修改資料

把剛才新增的請求方式改為PUT,就是修改了。不過修改必須指定id,

id對應文檔存在,則修改

id對應文檔不存在,則新增

比如,我們把id為3的資料進行修改:

PUT /heima/goods/3
{
    "title":"超大米手機",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":3899.00,
    "stock": 100,
    "saleable":true
}
           

結果:

{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 9,
    "successful": 9,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "超大米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 3899,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}
           

2.8.删除資料

删除使用DELETE請求,同樣,需要根據id進行删除:

文法

DELETE /索引庫名/類型名/id值
           

示例:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

3.查詢

我們從4塊來講查詢:

基本查詢 
_source過濾 
結果過濾 
進階查詢 
排序 
           

3.1.基本查詢:

基本文法

GET /索引庫名/_search
{
    "query":{
        "查詢類型":{
            "查詢條件":"查詢條件值"
        }
    }
}
           

這裡的query代表一個查詢對象,裡面可以有不同的查詢屬性

查詢類型:

例如:match_all,?match,term?,?range 等等

查詢條件:查詢條件會根據類型的不同,寫法也有差異,後面詳細講解

3.1.1 查詢所有(match_all)

示例:

GET /heima/_search
{
    "query":{
        "match_all": {}
    }
}
           

query:代表查詢對象

match_all:代表查詢所有

結果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "大米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2899
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "r9c1KGMBIhaxtY5rlRKv",
        "_score": 1,
        "_source": {
          "title": "小米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}
           

took:查詢花費時間,機關是毫秒

time_out:是否逾時

_shards:分片資訊

hits:搜尋結果總覽對象

total:搜尋到的總條數

max_score:所有結果中文檔得分的最高分

hits:搜尋結果的文檔對象數組,每個元素是一條搜尋到的文檔資訊

_index:索引庫

_type:文檔類型

_id:文檔id

_score:文檔得分

_source:文檔的源資料

3.1.2 比對查詢(match)

我們先加入一條資料,便于測試:

PUT /heima/goods/3
{
    "title":"小米電視4A",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":3899.00
}
           

現在,索引庫中有2部手機,1台電視:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

or關系

match類型查詢,會把查詢條件進行分詞,然後進行查詢,多個詞條之間是or的關系

GET /heima/_search
{
    "query":{
        "match":{
            "title":"小米電視"
        }
    }
}
           

結果:

"hits": {
    "total": 2,
    "max_score": 0.6931472,
    "hits": [
        {
            "_index": "heima",
            "_type": "goods",
            "_id": "tmUBomQB_mwm6wH_EC1-",
            "_score": 0.6931472,
            "_source": {
                "title": "小米手機",
                "images": "http://image.leyou.com/12479122.jpg",
                "price": 2699
            }
        },
        {
            "_index": "heima",
            "_type": "goods",
            "_id": "3",
            "_score": 0.5753642,
            "_source": {
                "title": "小米電視4A",
                "images": "http://image.leyou.com/12479122.jpg",
                "price": 3899
            }
        }
    ]
}
           

在上面的案例中,不僅會查詢到電視,而且與小米相關的都會查詢到,多個詞之間是or的關系。

and關系

某些情況下,我們需要更精确查找,我們希望這個關系變成and,可以這樣做:

GET /heima/_search
{
    "query":{
        "match": {
          "title": {
            "query": "小米電視",
            "operator": "and"
          }
        }
    }
}
           

結果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 0.5753642,
        "_source": {
          "title": "小米電視4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 3899
        }
      }
    ]
  }
}
           

本例中,隻有同時包含小米和電視的詞條才會被搜尋到。

or和and之間?

在 or 與 and 間二選一有點過于非黑即白。 如果使用者給定的條件分詞後有 5 個查詢詞項,想查找隻包含其中 4 個詞的文檔,該如何處理?将 operator 操作符參數設定成 and 隻會将此文檔排除。

有時候這正是我們期望的,但在全文搜尋的大多數應用場景下,我們既想包含那些可能相關的文檔,同時又排除那些不太相關的。換句話說,我們想要處于中間某種結果。

match 查詢支援 minimum_should_match 最小比對參數, 這讓我們可以指定必須比對的詞項數用來表示一個文檔是否相關。我們可以将其設定為某個具體數字,更常用的做法是将其設定為一個百分數,因為我們無法控制使用者搜尋時輸入的單詞數量:

GET /heima/_search
{
    "query":{
        "match":{
            "title":{
                "query":"小米曲面電視",
                "minimum_should_match": "75%"
            }
        }
    }
}
           

本例中,搜尋語句可以分為3個詞,如果使用and關系,需要同時滿足3個詞才會被搜尋到。這裡我們采用最獨幕喜劇牌數:75%,那麼也就是說隻要比對到總詞條數量的75%即可,這裡3*75% 約等于2。是以隻要包含2個詞條就算滿足條件了。

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

3.1.3 多字段查詢(multi_match)

multi_match與match類似,不同的是它可以在多個字段中查詢

GET /heima/_search
{
    "query":{
        "multi_match": {
            "query":    "小米",
            "fields":   [ "title", "subTitle" ]
        }
    }
}
           

本例中,我們會在title字段和subtitle字段中查詢小米這個詞

3.1.4 詞條比對(term)

term 查詢被用于精确值 比對,這些精确值可能是數字、時間、布爾或者那些未分詞的字元串

GET /heima/_search
{
    "query":{
        "term":{
            "price":2699.00
        }
    }
}
           

結果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "r9c1KGMBIhaxtY5rlRKv",
        "_score": 1,
        "_source": {
          "title": "小米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}
           

3.1.5 多詞條精确比對(terms)

terms 查詢和 term 查詢一樣,但它允許你指定多值進行比對。如果這個字段包含了指定值中的任何一個值,那麼這個文檔滿足條件:

GET /heima/_search
{
    "query":{
        "terms":{
            "price":[2699.00,2899.00,3899.00]
        }
    }
}
           

結果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "大米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2899
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "r9c1KGMBIhaxtY5rlRKv",
        "_score": 1,
        "_source": {
          "title": "小米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "小米電視4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 3899
        }
      }
    ]
  }
}
           

3.2.結果過濾

預設情況下,elasticsearch在搜尋的結果中,會把文檔中儲存在_source的所有字段都傳回。

如果我們隻想擷取其中的部分字段,我們可以添加_source的過濾

3.2.1.直接指定字段

示例:

GET /heima/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "price": 2699
    }
  }
}
           

傳回的結果:

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "r9c1KGMBIhaxtY5rlRKv",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米手機"
        }
      }
    ]
  }
}
           

3.2.2.指定includes和excludes

我們也可以通過:

includes:來指定想要顯示的字段

excludes:來指定不想要顯示的字段

二者都是可選的。

示例:

GET /heima/_search
{
  "_source": {
    "includes":["title","price"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}
           

與下面的結果将是一樣的:

GET /heima/_search
{
  "_source": {
     "excludes": ["images"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}
           

3.3 進階查詢

3.3.1 布爾組合(bool)

bool把各種其它查詢通過must(與)、must_not(非)、should(或)的方式進行組合

GET /heima/_search
{
    "query":{
        "bool":{
            "must":     { "match": { "title": "大米" }},
            "must_not": { "match": { "title":  "電視" }},
            "should":   { "match": { "title": "手機" }}
        }
    }
}
           

結果:

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "title": "大米手機",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2899
        }
      }
    ]
  }
}
           

3.3.2 範圍查詢(range)

range 查詢找出那些落在指定區間内的數字或者時間

GET /heima/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  1000.0,
                "lt":   2800.00
            }
        }
    }
}
           

range查詢允許以下字元:

操作符 說明

gt 大于 
gte 大于等于 
lt 小于 
lte 小于等于 
           

3.3.3 模糊查詢(fuzzy)

我們新增一個商品:

POST /heima/goods/4
{
    "title":"apple手機",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":6899.00
}
           

fuzzy 查詢是 term 查詢的模糊等價。它允許使用者搜尋詞條與實際詞條的拼寫出現偏差,但是偏差的編輯距離不得超過2:

GET /heima/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}
           

上面的查詢,也能查詢到apple手機

我們可以通過fuzziness來指定允許的編輯距離:

GET /heima/_search
{
  "query": {
    "fuzzy": {
        "title": {
            "value":"appla",
            "fuzziness":1
        }
    }
  }
}
           

3.4 過濾(filter)

條件查詢中進行過濾

所有的查詢都會影響到文檔的評分及排名。如果我們需要在查詢結果中進行過濾,并且不希望過濾條件影響評分,那麼就不要把過濾條件作為查詢條件來用。而是使用filter方式:

GET /heima/_search
{
    "query":{
        "bool":{
            "must":{ "match": { "title": "小米手機" }},
            "filter":{
                "range":{"price":{"gt":2000.00,"lt":3800.00}}
            }
        }
    }
}
           

注意:filter中還可以再次進行bool組合條件過濾。

無查詢條件,直接過濾

如果一次查詢隻有過濾,沒有查詢條件,不希望進行評分,我們可以使用constant_score取代隻有 filter 語句的 bool 查詢。在性能上是完全相同的,但對于提高查詢簡潔性和清晰度有很大幫助。

GET /heima/_search
{
    "query":{
        "constant_score":   {
            "filter": {
                 "range":{"price":{"gt":2000.00,"lt":3000.00}}
            }
        }
}
           

3.5 排序

3.4.1 單字段排序

sort 可以讓我們按照不同的字段進行排序,并且通過order指定排序的方式

GET /heima/_search
{
  "query": {
    "match": {
      "title": "小米手機"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
           

3.4.2 多字段排序

假定我們想要結合使用 price和 _score(得分) 進行查詢,并且比對的結果首先按照價格排序,然後按照相關性得分排序:

GET /goods/_search
{
    "query":{
        "bool":{
            "must":{ "match": { "title": "小米手機" }},
            "filter":{
                "range":{"price":{"gt":200000,"lt":300000}}
            }
        }
    },
    "sort": [
      { "price": { "order": "desc" }},
      { "_score": { "order": "desc" }}
    ]
}
           
  1. 聚合aggregations

    聚合可以讓我們極其友善的實作對資料的統計、分析。例如:

什麼品牌的手機最受歡迎?

這些手機的平均價格、最高價格、最低價格?

這些手機每月的銷售情況如何?

實作這些統計功能的比資料庫的sql要友善的多,而且查詢速度非常快,可以實作實時搜尋效果。

4.1 基本概念

Elasticsearch中的聚合,包含多種類型,最常用的兩種,一個叫桶,一個叫度量:

桶(bucket)

桶的作用,是按照某種方式對資料進行分組,每一組資料在ES中稱為一個桶,例如我們根據國籍對人劃分,可以得到中國桶、英國桶,日本桶……或者我們按照年齡段對人進行劃分:010,1020,2030,3040等。

Elasticsearch中提供的劃分桶的方式有很多:

Date Histogram Aggregation:根據日期階梯分組,例如給定階梯為周,會自動每周分為一組

Histogram Aggregation:根據數值階梯分組,與日期類似

Terms Aggregation:根據詞條内容分組,詞條内容完全比對的為一組

Range Aggregation:數值和日期的範圍分組,指定開始和結束,然後按段分組

……

綜上所述,我們發現bucket aggregations 隻負責對資料進行分組,并不進行計算,是以往往bucket中往往會嵌套另一種聚合:metrics aggregations即度量

度量(metrics)

分組完成以後,我們一般會對組中的資料進行聚合運算,例如求平均值、最大、最小、求和等,這些在ES中稱為度量

比較常用的一些度量聚合方式:

Avg Aggregation:求平均值 
Max Aggregation:求最大值 
Min Aggregation:求最小值 
Percentiles Aggregation:求百分比 
Stats Aggregation:同時傳回avg、max、min、sum、count等 
Sum Aggregation:求和 
Top hits Aggregation:求前幾 
Value Count Aggregation:求總數 
…… 
           

為了測試聚合,我們先批量導入一些資料

建立索引:

PUT /cars
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "transactions": {
      "properties": {
        "color": {
          "type": "keyword"
        },
        "make": {
          "type": "keyword"
        }
      }
    }
  }
}
           

注意:在ES中,需要進行聚合、排序、過濾的字段其處理方式比較特殊,是以不能被分詞。這裡我們将color和make這兩個文字類型的字段設定為keyword類型,這個類型不會被分詞,将來就可以參與聚合

導入資料

POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
           

4.2 聚合為桶

首先,我們按照 汽車的顔色color來劃分桶

GET /cars/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            }
        }
    }
}
           

size: 查詢條數,這裡設定為0,因為我們不關心搜尋到的資料,隻關心聚合結果,提高效率

aggs:聲明這是一個聚合查詢,是aggregations的縮寫

popular_colors:給這次聚合起一個名字,任意。

terms:劃分桶的方式,這裡是根據詞條劃分

field:劃分桶的字段

結果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4
        },
        {
          "key": "blue",
          "doc_count": 2
        },
        {
          "key": "green",
          "doc_count": 2
        }
      ]
    }
  }
}
           

hits:查詢結果為空,因為我們設定了size為0

aggregations:聚合的結果

popular_colors:我們定義的聚合名稱

buckets:查找到的桶,每個不同的color字段值都會形成一個桶

key:這個桶對應的color字段的值

doc_count:這個桶中的文檔數量

通過聚合的結果我們發現,目前紅色的小車比較暢銷!

4.3 桶内度量

前面的例子告訴我們每個桶裡面的文檔數量,這很有用。 但通常,我們的應用需要提供更複雜的文檔度量。 例如,每種顔色汽車的平均價格是多少?

是以,我們需要告訴Elasticsearch使用哪個字段,使用何種度量方式進行運算,這些資訊要嵌套在桶内,度量的運算會基于桶内的文檔進行

現在,我們為剛剛的聚合結果添加 求價格平均值的度量:

GET /cars/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            },
            "aggs":{
                "avg_price": { 
                   "avg": {
                      "field": "price" 
                   }
                }
            }
        }
    }
}
           

aggs:我們在上一個aggs(popular_colors)中添加新的aggs。可見度量也是一個聚合,度量是在桶内的聚合

avg_price:聚合的名稱

avg:度量的類型,這裡是求平均值

field:度量運算的字段

結果:

...
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4,
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "blue",
          "doc_count": 2,
          "avg_price": {
            "value": 20000
          }
        },
        {
          "key": "green",
          "doc_count": 2,
          "avg_price": {
            "value": 21000
          }
        }
      ]
    }
  }
...
           

可以看到每個桶中都有自己的avg_price字段,這是度量聚合的結果

4.4 桶内嵌套桶

剛剛的案例中,我們在桶内嵌套度量運算。事實上桶不僅可以嵌套運算, 還可以再嵌套其它桶。也就是說在每個分組中,再分更多組。

比如:我們想統計每種顔色的汽車中,分别屬于哪個制造商,按照make字段再進行分桶

GET /cars/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            },
            "aggs":{
                "avg_price": { 
                   "avg": {
                      "field": "price" 
                   }
                },
                "maker":{
                    "terms":{
                        "field":"make"
                    }
                }
            }
        }
    }
}
           

原來的color桶和avg計算我們不變

maker:在嵌套的aggs下新添一個桶,叫做maker

terms:桶的劃分類型依然是詞條

filed:這裡根據make字段進行劃分

部分結果:

...
{"aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "honda",
                "doc_count": 3
              },
              {
                "key": "bmw",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "blue",
          "doc_count": 2,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "ford",
                "doc_count": 1
              },
              {
                "key": "toyota",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 20000
          }
        },
        {
          "key": "green",
          "doc_count": 2,
          "maker": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "ford",
                "doc_count": 1
              },
              {
                "key": "toyota",
                "doc_count": 1
              }
            ]
          },
          "avg_price": {
            "value": 21000
          }
        }
      ]
    }
  }
}
           

我們可以看到,新的聚合maker被嵌套在原來每一個color的桶中。

每個顔色下面都根據 make字段進行了分組

我們能讀取到的資訊:

紅色車共有4輛

紅色車的平均售價是 $32,500 美元。

其中3輛是 Honda 本田制造,1輛是 BMW 寶馬制造。

4.5.劃分桶的其它方式

前面講了,劃分桶的方式有很多,例如:

Date Histogram Aggregation:根據日期階梯分組,例如給定階梯為周,會自動每周分為一組

Histogram Aggregation:根據數值階梯分組,與日期類似

Terms Aggregation:根據詞條内容分組,詞條内容完全比對的為一組

Range Aggregation:數值和日期的範圍分組,指定開始和結束,然後按段分組

剛剛的案例中,我們采用的是Terms Aggregation,即根據詞條劃分桶。

接下來,我們再學習幾個比較實用的:

4.5.1.階梯分桶Histogram

原理:

histogram是把數值類型的字段,按照一定的階梯大小進行分組。你需要指定一個階梯值(interval)來劃分階梯大小。

舉例:

比如你有價格字段,如果你設定interval的值為200,那麼階梯就會是這樣的:

0,200,400,600,…

上面列出的是每個階梯的key,也是區間的啟點。

如果一件商品的價格是450,會落入哪個階梯區間呢?計算公式如下:

bucket_key = Math.floor((value - offset) / interval) * interval + offset

value:就是目前資料的值,本例中是450

offset:起始偏移量,預設為0

interval:階梯間隔,比如200

是以你得到的key = Math.floor((450 - 0) / 200) * 200 + 0 = 400

操作一下:

比如,我們對汽車的價格進行分組,指定間隔interval為5000:

GET /cars/_search

{
  "size":0,
  "aggs":{
    "price":{
      "histogram": {
        "field": "price",
        "interval": 5000
      }
    }
  }
}
           

結果:

{
  "took": 21,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price": {
      "buckets": [
        {
          "key": 10000,
          "doc_count": 2
        },
        {
          "key": 15000,
          "doc_count": 1
        },
        {
          "key": 20000,
          "doc_count": 2
        },
        {
          "key": 25000,
          "doc_count": 1
        },
        {
          "key": 30000,
          "doc_count": 1
        },
        {
          "key": 35000,
          "doc_count": 0
        },
        {
          "key": 40000,
          "doc_count": 0
        },
        {
          "key": 45000,
          "doc_count": 0
        },
        {
          "key": 50000,
          "doc_count": 0
        },
        {
          "key": 55000,
          "doc_count": 0
        },
        {
          "key": 60000,
          "doc_count": 0
        },
        {
          "key": 65000,
          "doc_count": 0
        },
        {
          "key": 70000,
          "doc_count": 0
        },
        {
          "key": 75000,
          "doc_count": 0
        },
        {
          "key": 80000,
          "doc_count": 1
        }
      ]
    }
  }
}
           

你會發現,中間有大量的文檔數量為0 的桶,看起來很醜。

我們可以增加一個參數min_doc_count為1,來限制最少文檔數量為1,這樣文檔數量為0的桶會被過濾

示例:

GET /cars/_search
{
  "size":0,
  "aggs":{
    "price":{
      "histogram": {
        "field": "price",
        "interval": 5000,
        "min_doc_count": 1
      }
    }
  }
}
           

結果:

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price": {
      "buckets": [
        {
          "key": 10000,
          "doc_count": 2
        },
        {
          "key": 15000,
          "doc_count": 1
        },
        {
          "key": 20000,
          "doc_count": 2
        },
        {
          "key": 25000,
          "doc_count": 1
        },
        {
          "key": 30000,
          "doc_count": 1
        },
        {
          "key": 80000,
          "doc_count": 1
        }
      ]
    }
  }
}
           

完美,!

如果你用kibana将結果變為柱形圖,會更好看:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

4.5.2.範圍分桶range

範圍分桶與階梯分桶類似,也是把數字按照階段進行分組,隻不過range方式需要你自己指定每一組的起始和結束大小。

5.Spring Data Elasticsearch

Elasticsearch提供的Java用戶端有一些不太友善的地方:

很多地方需要拼接Json字元串,在java中拼接字元串有多恐怖你應該懂的

需要自己把對象序列化為json存儲

查詢到結果也需要自己反序列化為對象

是以,我們這裡就不講解原生的Elasticsearch用戶端API了。

而是學習Spring提供的套件:Spring Data Elasticsearch。

5.1.簡介

Spring Data Elasticsearch是Spring Data項目下的一個子子產品。

檢視 Spring Data的官網:http://projects.spring.io/spring-data/

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

Spring Data的使命是為資料通路提供熟悉且一緻的基于Spring的程式設計模型,同時仍保留底層資料存儲的特殊特性。

它使得使用資料通路技術,關系資料庫和非關系資料庫,map-reduce架構和基于雲的資料服務變得容易。這是一個總括項目,其中包含許多特定于給定資料庫的子項目。這些令人興奮的技術項目背後,是由許多公司和開發人員合作開發的。

Spring Data 的使命是給各種資料通路提供統一的程式設計接口,不管是關系型資料庫(如MySQL),還是非關系資料庫(如Redis),或者類似Elasticsearch這樣的索引資料庫。進而簡化開發人員的代碼,提高開發效率。

包含很多不同資料操作的子產品:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

Spring Data Elasticsearch的頁面:https://projects.spring.io/spring-data-elasticsearch/

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

特征:

支援Spring的基于@Configuration的java配置方式,或者XML配置方式

提供了用于操作ES的便捷工具類ElasticsearchTemplate。包括實作文檔到POJO之間的自動智能映射。

利用Spring的資料轉換服務實作的功能豐富的對象映射

基于注解的中繼資料映射方式,而且可擴充以支援更多不同的資料格式

根據持久層接口自動生成對應實作方法,無需人工編寫基本操作代碼(類似mybatis,根據接口自動得到實作)。當然,也支援人工定制查詢

5.2.建立Demo工程

我們建立一個demo,學習Elasticsearch

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋
JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

pom依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leyou.demo</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>elasticsearch</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
           

application.yml檔案配置:

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 192.168.56.101:9300
           

5.3.實體類及注解

首先我們準備好實體類:

public class Item {
    Long id;
    String title; //标題
    String category;// 分類
    String brand; // 品牌
    Double price; // 價格
    String images; // 圖檔位址
}
           

映射

Spring Data通過注解來聲明字段的映射屬性,有下面的三個注解:

@Document 作用在類,标記實體類為文檔對象,一般有兩個屬性

indexName:對應索引庫名稱

type:對應在索引庫中的類型

shards:分片數量,預設5

replicas:副本數量,預設1

@Id 作用在成員變量,标記一個字段作為id主鍵

@Field 作用在成員變量,标記為文檔的字段,并指定字段映射屬性:

type:字段類型,取值是枚舉:FieldType

index:是否索引,布爾類型,預設是true

store:是否存儲,布爾類型,預設是false

analyzer:分詞器名稱

示例:

@Document(indexName = "item",type = "docs", shards = 1, replicas = 0)
public class Item {
    @Id
    private Long id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title; //标題

    @Field(type = FieldType.Keyword)
    private String category;// 分類

    @Field(type = FieldType.Keyword)
    private String brand; // 品牌

    @Field(type = FieldType.Double)
    private Double price; // 價格

    @Field(index = false, type = FieldType.Keyword)
    private String images; // 圖檔位址
}
           

5.4.Template索引操作

5.4.1.建立索引和映射

建立索引

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

ElasticsearchTemplate中提供了建立索引的API:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

可以根據類的資訊自動生成,也可以手動指定indexName和Settings

映射

映射相關的API:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

可以根據類的位元組碼資訊(注解配置)來生成映射,或者手動編寫映射

我們這裡采用類的位元組碼資訊建立索引并映射:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ItcastElasticsearchApplication.class)
public class IndexTest {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Test
    public void testCreate(){
        // 建立索引,會根據Item類的@Document注解資訊來建立
        elasticsearchTemplate.createIndex(Item.class);
        // 配置映射,會根據Item類中的id、Field等字段來自動完成映射
        elasticsearchTemplate.putMapping(Item.class);
    }
}
           

結果:

GET /item
{
  "item": {
    "aliases": {},
    "mappings": {
      "docs": {
        "properties": {
          "brand": {
            "type": "keyword"
          },
          "category": {
            "type": "keyword"
          },
          "images": {
            "type": "keyword",
            "index": false
          },
          "price": {
            "type": "double"
          },
          "title": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    },
    "settings": {
      "index": {
        "refresh_interval": "1s",
        "number_of_shards": "1",
        "provided_name": "item",
        "creation_date": "1525405022589",
        "store": {
          "type": "fs"
        },
        "number_of_replicas": "0",
        "uuid": "4sE9SAw3Sqq1aAPz5F6OEg",
        "version": {
          "created": "6020499"
        }
      }
    }
  }
}
           

5.3.2.删除索引

删除索引的API:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

可以根據類名或索引名删除。

示例:

@Test
public void deleteIndex() {
    esTemplate.deleteIndex("heima");
}
           

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

5.4.Repository文檔操作

Spring Data 的強大之處,就在于你不用寫任何DAO處理,自動根據方法名或類的資訊進行CRUD操作。隻要你定義一個接口,然後繼承Repository提供的一些子接口,就能具備各種基本的CRUD功能。

我們隻需要定義接口,然後繼承它就OK了。

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

public interface ItemRepository extends ElasticsearchRepository<Item,Long> {

}

來看下Repository的繼承關系:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

我們看到有一個ElasticsearchRepository接口:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

5.4.1.新增文檔

@Autowired
private ItemRepository itemRepository;

@Test
public void index() {
    Item item = new Item(1L, "小米手機7", " 手機",
                         "小米", 3499.00, "http://image.leyou.com/13123.jpg");
    itemRepository.save(item);
}
           

去頁面查詢看看:

GET /item/_search
           

結果:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "item",
        "_type": "docs",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "title": "小米手機7",
          "category": " 手機",
          "brand": "小米",
          "price": 3499,
          "images": "http://image.leyou.com/13123.jpg"
        }
      }
    ]
  }
}
           

5.4.2.批量新增

代碼:

@Test
public void indexList() {
    List<Item> list = new ArrayList<>();
    list.add(new Item(2L, "堅果手機R1", " 手機", "錘子", 3699.00, "http://image.leyou.com/123.jpg"));
    list.add(new Item(3L, "華為META10", " 手機", "華為", 4499.00, "http://image.leyou.com/3.jpg"));
    // 接收對象集合,實作批量新增
    itemRepository.saveAll(list);
}
           

再次去頁面查詢:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "item",
        "_type": "docs",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "title": "堅果手機R1",
          "category": " 手機",
          "brand": "錘子",
          "price": 3699,
          "images": "http://image.leyou.com/13123.jpg"
        }
      },
      {
        "_index": "item",
        "_type": "docs",
        "_id": "3",
        "_score": 1,
        "_source": {
          "id": 3,
          "title": "華為META10",
          "category": " 手機",
          "brand": "華為",
          "price": 4499,
          "images": "http://image.leyou.com/13123.jpg"
        }
      },
      {
        "_index": "item",
        "_type": "docs",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "title": "小米手機7",
          "category": " 手機",
          "brand": "小米",
          "price": 3499,
          "images": "http://image.leyou.com/13123.jpg"
        }
      }
    ]
  }
}
           

5.4.3.修改文檔

修改和新增是同一個接口,區分的依據就是id,這一點跟我們在頁面發起PUT請求是類似的。

5.4.4.基本查詢

ElasticsearchRepository提供了一些基本的查詢方法:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

我們來試試查詢所有:

@Test
public void testFind(){
    // 查詢全部,并安裝價格降序排序
    Iterable<Item> items = this.itemRepository.findAll(Sort.by(Sort.Direction.DESC, "price"));
    items.forEach(item-> System.out.println(item));
}
           

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

5.4.5.自定義方法

Spring Data 的另一個強大功能,是根據方法名稱自動實作功能。

比如:你的方法名叫做:findByTitle,那麼它就知道你是根據title查詢,然後自動幫你完成,無需寫實作類。

當然,方法名稱要符合一定的約定:

Keyword Sample Elasticsearch Query String 
And findByNameAndPrice {"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} 
Or findByNameOrPrice {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}} 
Is findByName {"bool" : {"must" : {"field" : {"name" : "?"}}}} 
Not findByNameNot {"bool" : {"must_not" : {"field" : {"name" : "?"}}}} 
Between findByPriceBetween {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} 
LessThanEqual findByPriceLessThan {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} 
GreaterThanEqual findByPriceGreaterThan {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} 
Before findByPriceBefore {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}} 
After findByPriceAfter {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}} 
Like findByNameLike {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} 
StartingWith findByNameStartingWith {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}} 
EndingWith findByNameEndingWith {"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}} 
Contains/Containing findByNameContaining {"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}} 
In findByNameIn(Collection<String>names) {"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}} 
NotIn findByNameNotIn(Collection<String>names) {"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}} 
Near findByStoreNear Not Supported Yet ! 
True findByAvailableTrue {"bool" : {"must" : {"field" : {"available" : true}}}} 
False findByAvailableFalse {"bool" : {"must" : {"field" : {"available" : false}}}} 
OrderBy findByAvailableTrueOrderByNameDesc {"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}} 
           

例如,我們來按照價格區間查詢,定義這樣的一個方法:

public interface ItemRepository extends ElasticsearchRepository<Item,Long> {

    /**
     * 根據價格區間查詢
     * @param price1
     * @param price2
     * @return
     */
    List<Item> findByPriceBetween(double price1, double price2);
}
           

然後添加一些測試資料:

@Test
public void indexList() {
    List<Item> list = new ArrayList<>();
    list.add(new Item(1L, "小米手機7", "手機", "小米", 3299.00, "http://image.leyou.com/13123.jpg"));
    list.add(new Item(2L, "堅果手機R1", "手機", "錘子", 3699.00, "http://image.leyou.com/13123.jpg"));
    list.add(new Item(3L, "華為META10", "手機", "華為", 4499.00, "http://image.leyou.com/13123.jpg"));
    list.add(new Item(4L, "小米Mix2S", "手機", "小米", 4299.00, "http://image.leyou.com/13123.jpg"));
    list.add(new Item(5L, "榮耀V10", "手機", "華為", 2799.00, "http://image.leyou.com/13123.jpg"));
    // 接收對象集合,實作批量新增
    itemRepository.saveAll(list);
}
           

不需要寫實作類,然後我們直接去運作:

@Test
public void queryByPriceBetween(){
    List<Item> list = this.itemRepository.findByPriceBetween(2000.00, 3500.00);
    for (Item item : list) {
        System.out.println("item = " + item);
    }
}
           

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

雖然基本查詢和自定義方法已經很強大了,但是如果是複雜查詢(模糊、通配符、詞條查詢等)就顯得力不從心了。此時,我們隻能使用原生查詢。

5.5.進階查詢

5.5.1.基本查詢

先看看基本玩法

@Test
public void testQuery(){
    // 詞條查詢
    MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "小米");
    // 執行查詢
    Iterable<Item> items = this.itemRepository.search(queryBuilder);
    items.forEach(System.out::println);
}
           

Repository的search方法需要QueryBuilder參數,elasticSearch為我們提供了一個對象QueryBuilders:

1532008212626

QueryBuilders提供了大量的靜态方法,用于生成各種不同類型的查詢對象,例如:詞條、模糊、通配符等QueryBuilder對象。

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

elasticsearch提供很多可用的查詢方式,但是不夠靈活。如果想玩過濾或者聚合查詢等就很難了。

5.5.2.自定義查詢

先來看最基本的match query:

@Test
public void testNativeQuery(){
    // 建構查詢條件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 添加基本的分詞查詢
    queryBuilder.withQuery(QueryBuilders.matchQuery("title", "小米"));
    // 執行搜尋,擷取結果
    Page<Item> items = this.itemRepository.search(queryBuilder.build());
    // 列印總條數
    System.out.println(items.getTotalElements());
    // 列印總頁數
    System.out.println(items.getTotalPages());
    items.forEach(System.out::println);
}
           

NativeSearchQueryBuilder:Spring提供的一個查詢條件建構器,幫助建構json格式的請求體

Page:預設是分頁查詢,是以傳回的是一個分頁的結果對象,包含屬性:

totalElements:總條數

totalPages:總頁數

Iterator:疊代器,本身實作了Iterator接口,是以可直接疊代得到目前頁的資料

其它屬性:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

5.5.4.分頁查詢

利用NativeSearchQueryBuilder可以友善的實作分頁:

@Test
public void testNativeQuery(){
    // 建構查詢條件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 添加基本的分詞查詢
    queryBuilder.withQuery(QueryBuilders.termQuery("category", "手機"));

    // 初始化分頁參數
    int page = 0;
    int size = 3;
    // 設定分頁參數
    queryBuilder.withPageable(PageRequest.of(page, size));

    // 執行搜尋,擷取結果
    Page<Item> items = this.itemRepository.search(queryBuilder.build());
    // 列印總條數
    System.out.println(items.getTotalElements());
    // 列印總頁數
    System.out.println(items.getTotalPages());
    // 每頁大小
    System.out.println(items.getSize());
    // 目前頁
    System.out.println(items.getNumber());
    items.forEach(System.out::println);
}
           

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

可以發現,Elasticsearch中的分頁是從第0頁開始。

5.5.5.排序

排序也通用通過NativeSearchQueryBuilder完成:

@Test
public void testSort(){
    // 建構查詢條件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 添加基本的分詞查詢
    queryBuilder.withQuery(QueryBuilders.termQuery("category", "手機"));

    // 排序
    queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));

    // 執行搜尋,擷取結果
    Page<Item> items = this.itemRepository.search(queryBuilder.build());
    // 列印總條數
    System.out.println(items.getTotalElements());
    items.forEach(System.out::println);
}
           

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

5.6.聚合

5.6.1.聚合為桶

桶就是分組,比如這裡我們按照品牌brand進行分組:

@Test
public void testAgg(){
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 不查詢任何結果
    queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));
    // 1、添加一個新的聚合,聚合類型為terms,聚合名稱為brands,聚合字段為brand
    queryBuilder.addAggregation(
        AggregationBuilders.terms("brands").field("brand"));
    // 2、查詢,需要把結果強轉為AggregatedPage類型
    AggregatedPage<Item> aggPage = (AggregatedPage<Item>) this.itemRepository.search(queryBuilder.build());
    // 3、解析
    // 3.1、從結果中取出名為brands的那個聚合,
    // 因為是利用String類型字段來進行的term聚合,是以結果要強轉為StringTerm類型
    StringTerms agg = (StringTerms) aggPage.getAggregation("brands");
    // 3.2、擷取桶
    List<StringTerms.Bucket> buckets = agg.getBuckets();
    // 3.3、周遊
    for (StringTerms.Bucket bucket : buckets) {
        // 3.4、擷取桶中的key,即品牌名稱
        System.out.println(bucket.getKeyAsString());
        // 3.5、擷取桶中的文檔數量
        System.out.println(bucket.getDocCount());
    }

}
           

顯示的結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

關鍵API:

AggregationBuilders:聚合的建構工廠類。所有聚合都由這個類來建構,看看他的靜态方法:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

AggregatedPage:聚合查詢的結果類。它是Page的子接口:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

AggregatedPage在Page功能的基礎上,拓展了與聚合相關的功能,它其實就是對聚合結果的一種封裝,大家可以對照聚合結果的JSON結構來看。

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

而傳回的結果都是Aggregation類型對象,不過根據字段類型不同,又有不同的子類表示

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

我們看下頁面的查詢的JSON結果與Java類的對照關系:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋

5.6.2.嵌套聚合,求平均值

代碼:

@Test
public void testSubAgg(){
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 不查詢任何結果
    queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));
    // 1、添加一個新的聚合,聚合類型為terms,聚合名稱為brands,聚合字段為brand
    queryBuilder.addAggregation(
        AggregationBuilders.terms("brands").field("brand")
        .subAggregation(AggregationBuilders.avg("priceAvg").field("price")) // 在品牌聚合桶内進行嵌套聚合,求平均值
    );
    // 2、查詢,需要把結果強轉為AggregatedPage類型
    AggregatedPage<Item> aggPage = (AggregatedPage<Item>) this.itemRepository.search(queryBuilder.build());
    // 3、解析
    // 3.1、從結果中取出名為brands的那個聚合,
    // 因為是利用String類型字段來進行的term聚合,是以結果要強轉為StringTerm類型
    StringTerms agg = (StringTerms) aggPage.getAggregation("brands");
    // 3.2、擷取桶
    List<StringTerms.Bucket> buckets = agg.getBuckets();
    // 3.3、周遊
    for (StringTerms.Bucket bucket : buckets) {
        // 3.4、擷取桶中的key,即品牌名稱  3.5、擷取桶中的文檔數量
        System.out.println(bucket.getKeyAsString() + ",共" + bucket.getDocCount() + "台");

        // 3.6.擷取子聚合結果:
        InternalAvg avg = (InternalAvg) bucket.getAggregations().asMap().get("priceAvg");
        System.out.println("平均售價:" + avg.getValue());
    }

}
           

結果:

JAVA商城項目(微服務架構)——第11天 elasticsearch搜尋