中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

百度大數(shù)據(jù)官網(wǎng)入口seo網(wǎng)站關(guān)鍵詞快速排名

百度大數(shù)據(jù)官網(wǎng)入口,seo網(wǎng)站關(guān)鍵詞快速排名,一個企業(yè)網(wǎng)站建設(shè)需要多長時間,網(wǎng)上購物十大品牌ES 概述 ES 是一個開源的高擴展的分布式全文搜索引擎。 倒排索引 環(huán)境準備 Elasticsearch 官方地址:https://www.elastic.co/cn/ 下載地址: 注意:9300 端口為 Elasticsearch 集群間組件的通信端口,9200 端口為瀏覽器訪問的 h…

ES 概述

ES 是一個開源的高擴展的分布式全文搜索引擎。

倒排索引

環(huán)境準備

Elasticsearch 官方地址:https://www.elastic.co/cn/

下載地址:

注意:9300 端口為 Elasticsearch 集群間組件的通信端口,9200 端口為瀏覽器訪問的 http

在瀏覽器中訪問:http://localhost:9200

ES VS Mysql

與 MySQL 中概念對比

Types 的概念已經(jīng)被逐漸弱化,Elasticsearch 6.X 中,一個 index 下已經(jīng)只能包含一個 type,Elasticsearch 7.X 中, Type 的概念已經(jīng)被刪除了。

版本Type
5.x支持多個 type
6.x只能有一種 type
7.x默認不再支持自定義 type (默認類型為:_doc)

ES 操作

GET,PUT,DELTE,HEAD 操作具有冪等性,POST 操作不具有冪等性。

DSL 其實是 Domain Specific Language 的縮寫,中文翻譯為領(lǐng)域特定語言。

索引操作

# 創(chuàng)建索引
PUT shopping
{"acknowledged"【響應(yīng)結(jié)果】: true, # true 操作成功"shards_acknowledged"【分片結(jié)果】: true, # 分片操作成功"index"【索引名稱】: "shopping"
}

注意:創(chuàng)建索引庫的分片數(shù)默認 1 片,在 7.0.0 之前的 Elasticsearch 版本中,默認 5 片

# 查看索引
GET shopping
{"shopping"【索引名】: {"aliases"【別名】: {},"mappings"【映射】: {},"settings"【設(shè)置】: {"index"【設(shè)置 - 索引】: {"creation_date"【設(shè)置 - 索引 - 創(chuàng)建時間】: "1614265373911","number_of_shards"【設(shè)置 - 索引 - 主分片數(shù)量】: "1","number_of_replicas"【設(shè)置 - 索引 - 副分片數(shù)量】: "1","uuid"【設(shè)置 - 索引 - 唯一標識】: "eI5wemRERTumxGCc1bAk2A","version"【設(shè)置 - 索引 - 版本】: {"created": "7080099"},"provided_name"【設(shè)置 - 索引 - 名稱】: "shopping"}}}
}
# 刪除索引
DELETE shopping

文檔操作

創(chuàng)建文檔

# 自動生成 ID
POST shopping/_doc
{"title": "小米手機","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999
}# 指定 ID(具有冪等性可以使用 PUT 命令)
PUT shopping/_doc/1001
{"title": "小米手機","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999
}

文檔檢索

# 全部查詢
GET shopping/_doc/_search# 主鍵查詢
GET shopping/_doc/1001

修改文檔

# 全量修改
PUT shopping/_doc/1001
{"title": "小米手機2","category": "小米2","images": "http://www.gulixueyuan.com/xm.jpg","price": 4999
}

ES 的 update 只是在ES內(nèi)部查詢出來后,再覆蓋。excludes 的字段的數(shù)據(jù)會丟失。

# 局部修改
POST shopping/_update/1001
{"doc": {"title":"華為手機"}
}

刪除文檔

DELETE shopping/_doc/9H58aXwBfxge3XJyFrMl

高級查詢

條件查詢

# 條件查詢
# select * from shopping where category='小米'
# match 會把 query 進行分詞,多個詞之間是 or 關(guān)系
GET shopping/_search
{"query": {"match": {"category": "小米"}}
}# 分頁查詢
# select title,price from shopping where category='小米' order by price desc limit 0,2
GET shopping/_search
{"query": {"match": {"category": "小米"}},"from": 0,"size": 2,"sort": [{"price": {"order": "desc"}}],"_source": ["title","price"]
}

多條件查詢:and or

# select * from shopping where category='小米' and price>= 5000
GET shopping/_search
{"query": {"bool": {"must": [{"match": {"category": "小米"}},{# 范圍查詢"range": {"price": {"gte": 5000}}}]}}
}# select * from shopping where category like '%小米%' or category like '%華為%'
GET shopping/_search
{"query": {"bool": {"should": [{"match": {"category": "小米"}},{"match": {"category": "華為"}}]}}
}# select * from shopping where not category like '%小米%'
GET shopping/_search
{"query": {"bool": {"must_not": [{"match": {"category": "小米"}}]}}
}# select * from shopping where category like '%手機%' or title like '%手機%'
# multi_match 與 match 類似,不同的是它可以在多個字段中查詢
GET shopping/_search
{"query": {"multi_match": {"query": "手機","fields": ["category","title"]}}
}# 中文分詞
GET _analyze
{"text": ["小米","華為"]
}# 中文分詞
GET _analyze
{"text": ["Elasticsearch built-in security"]
}GET _analyze
{"analyzer": "ik_smart","text": ["小米","華為"]
}GET _analyze
{"analyzer": "ik_smart","text":"中華人民共和國國歌"
}GET _analyze
{"analyzer": "ik_max_word","text":"中華人民共和國國歌"
}

IK 分詞器

  1. 下載:https://github.com/medcl/elasticsearch-analysis-ik/releases
  2. 解壓:拷貝到 Elasticsearch 的 plugins 目錄下:文件夾名稱為 ik
  3. 重啟:Elasticsearch

聚合查詢

# price 平均值
# select avg(price) as price_avg from shopping
GET shopping/_search
{"aggs": {"price_avg": {"avg": {"field": "price"}}},"size": 0
}# price 最小值
# select min(price) as price_min from shopping
# avg,min,max,sum
GET shopping/_search
{"aggs": {"price_min": {"min": {"field": "price"}}},"size": 0
}# 同時返回:count,min,max,avg,sum
GET shopping/_search
{"aggs": {"stats_price": {"stats": {"field": "price"}}},"size": 0
}# select price as key,count(1) as doc_count  from shopping group by price
GET shopping/_search
{"aggs": {"category_group": {"terms": {"field": "price"}}},"size": 0
}

映射關(guān)系

# name:分詞并建倒排索引
# sex:不分詞,建倒排索引
# tel:不建倒排索引
PUT user
{"mappings": {"properties": {"name": {"type": "text","index": true},"sex": {"type": "keyword","index": true},"tel": {"type": "text","index": false }}}
}# 查看索引 mapping
GET user/_mapping# 插入測試數(shù)據(jù)
POST user/_bulk
{"index":{"_id":"1001"}}
{"name":"張三","sex":"男生","tel":"1111"}
{"index":{"_id":"1002"}}
{"name":"李四","sex":"男生","tel":"2222"}
{"index":{"_id":"1003"}}
{"name":"王五","sex":"女生","tel":"3333"}GET user/_search
{"query": {"match": {"name": "張"}}
}GET user/_search
{"query": {"match": {"sex": "男"}}
}GET user/_search
{"query": {"match": {"tel": "1111"}}
}# keyword 可以聚合
GET user/_search
{"aggs": {"sex_group": {"terms": {"field": "sex"}}},"size": 0
}# text 不可以聚合
GET user/_search
{"aggs": {"name_group": {"terms": {"field": "name"}}},"size": 0
}

常見 type 類型

  • String 類型
    • text:可分詞
    • keyword:不可分詞,數(shù)據(jù)會作為完整字段進行匹配
  • Numerica:數(shù)值型
    • 基本數(shù)據(jù)類型:long、integer、short、byte、double、float、half_float
    • 浮點的高精度類型:sacled_float
  • Date:日期類型
  • Array:數(shù)組類型
  • Object:對象

Java API 操作

依賴

 <dependencies><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.8.0</version></dependency><!-- elasticsearch 的客戶端 --><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.8.0</version></dependency><!-- elasticsearch 依賴 2.x 的 log4j --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.8.2</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.78</version></dependency><!-- junit 單元測試 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>

環(huán)境測試

public class EsClient {public static void main(String[] args) throws IOException {// 創(chuàng)建 ES 客戶端RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));// 關(guān)閉 ES 客戶端esClient.close();}
}

索引操作

創(chuàng)建索引

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));CreateIndexRequest request = new CreateIndexRequest("user_v1");CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());esClient.close();}

查詢索引信息

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));GetIndexRequest request = new GetIndexRequest("user_v1");GetIndexResponse response = esClient.indices().get(request, RequestOptions.DEFAULT);System.out.println(response.getMappings());System.out.println(response.getAliases());System.out.println(response.getSettings());esClient.close();}

刪除索引信息

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));DeleteIndexRequest request = new DeleteIndexRequest("user_v1");AcknowledgedResponse delete = esClient.indices().delete(request, RequestOptions.DEFAULT);System.out.println(delete.isAcknowledged());esClient.close();}

文檔操作

創(chuàng)建文檔

public class EsDocCreate {public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));IndexRequest request = new IndexRequest("user_v1");request.id("1003");User user = new User("張三", "男生", "1111");request.source(JSON.toJSONString(user), XContentType.JSON);IndexResponse index = esClient.index(request, RequestOptions.DEFAULT);System.out.println(index.getResult());esClient.close();}

局部修改

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));// 局部修改UpdateRequest request = new UpdateRequest("user_v1", "1003");request.doc(XContentType.JSON, "name", "zhangsan");UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);System.out.println(response.getResult());esClient.close();}

根據(jù)ID 檢索文檔

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));GetRequest request = new GetRequest("user_v1", "1003");GetResponse response = esClient.get(request, RequestOptions.DEFAULT);System.out.println(response.getSource());esClient.close();}

文檔刪除

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));DeleteRequest request = new DeleteRequest("user_v1", "1002");DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);System.out.println(response.getResult());esClient.close();}

批量更新

將操作打包,批量發(fā)送給 ES 集群。

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));BulkRequest request = new BulkRequest();// 新增IndexRequest indexRequest = new IndexRequest("user_v1");indexRequest.id("1004");indexRequest.source(JSON.toJSONString(new User("李四", "男生", "4444")), XContentType.JSON);// 新增IndexRequest indexRequest2 = new IndexRequest("user_v1");indexRequest2.id("1005");indexRequest2.source(JSON.toJSONString(new User("王五", "女生", "5555")), XContentType.JSON);// 刪除DeleteRequest deleteRequest = new DeleteRequest("user_v1", "1001");request.add(indexRequest);request.add(indexRequest2);request.add(deleteRequest);BulkResponse responses = esClient.bulk(request, RequestOptions.DEFAULT);System.out.println(responses.getItems());esClient.close();}

高級檢索

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest request = new SearchRequest("user_v1");// 檢索全部數(shù)據(jù)request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);for (SearchHit searchHit : response.getHits()) {System.out.println(searchHit.getSourceAsString());}esClient.close();}
    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest request = new SearchRequest("shopping");request.source(new SearchSourceBuilder().query(QueryBuilders.matchQuery("category", "小米")).from(0) // 分頁.size(10).sort("price", SortOrder.DESC) // 排序);//        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("category","小米")));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);for (SearchHit searchHit : response.getHits()) {System.out.println(searchHit.getSourceAsString());}esClient.close();}

多條件檢索

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));// 構(gòu)建查詢的請求體SearchSourceBuilder sourceBuilder  = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();// andboolQueryBuilder.must(QueryBuilders.matchQuery("category","小米"));// notboolQueryBuilder.mustNot(QueryBuilders.matchQuery("price","5999"));// orboolQueryBuilder.should(QueryBuilders.matchQuery("category","華為"));sourceBuilder.query(boolQueryBuilder);SearchRequest request = new SearchRequest("shopping");request.source(sourceBuilder);SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);for (SearchHit searchHit : response.getHits()) {System.out.println(searchHit.getSourceAsString());}esClient.close();}
    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));// 構(gòu)建高亮字段HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.preTags("<font color='red'>");highlightBuilder.postTags("</font>");highlightBuilder.field("name");SearchRequest request = new SearchRequest("shopping");request.source(new SearchSourceBuilder().query(QueryBuilders.rangeQuery("price").gt(0)   // 范圍查詢.lt(6000)).highlighter(highlightBuilder));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);for (SearchHit searchHit : response.getHits()) {System.out.println(searchHit.getSourceAsString());}esClient.close();}

聚合

    public static void main(String[] args) throws IOException {RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest request = new SearchRequest("shopping");request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.max("maxPrice").field("price")));
//   request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.min("minPrice").field("price")));
//   request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("avgPrice").field("price")));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);if (response.getAggregations().iterator().hasNext()) {ParsedMax parsedMax = (ParsedMax) response.getAggregations().iterator().next();
//            ParsedMin parsedMin = (ParsedMin) response.getAggregations().iterator().next();
//            ParsedAvg parsedAvg = (ParsedAvg) response.getAggregations().iterator().next();System.out.println(parsedMax.getValue());}System.out.println(response);esClient.close();}

分組聚合

        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));SearchRequest request = new SearchRequest("shopping");request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.terms("price_group").field("price")));SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);if (response.getAggregations().iterator().hasNext()) {ParsedLongTerms parsedMax = (ParsedLongTerms) response.getAggregations().iterator().next();for (Terms.Bucket bucket : parsedMax.getBuckets()) {System.out.println(bucket.getKey() + "\t" + bucket.getDocCount());}}esClient.close();

ES 集群

單點服務(wù)器的問題:

  • 存儲容量有限
  • 容易出現(xiàn)單點故障,無法實現(xiàn)高可用
  • 并發(fā)處理能力有限

搭建集群

修改配置文件

node.master:表示節(jié)點是否具有成為主節(jié)點的資格。

node.data:表示節(jié)點是否存儲數(shù)據(jù)。

Node 節(jié)點組合:

  • 主節(jié)點 + 數(shù)據(jù)節(jié)點(master + data)即有稱為主節(jié)點的資格,又存儲數(shù)據(jù)
  • 數(shù)據(jù)節(jié)點(data):不參與選舉,只會存儲數(shù)據(jù)
  • 客戶端節(jié)點(client):不會成為主節(jié)點,也不會存儲數(shù)據(jù),主要是針對海量請求的時候,可以進行負載均衡

一個 Mac 上起 3 es 進程

添加如下配置:config/elasticsearch.yml

節(jié)點1 配置

# 加入如下配置
# 集群名稱
cluster.name: my-application
# 節(jié)點名稱,每個節(jié)點的名稱不能重復(fù) 
node.name: node-01
# 是不是有資格主節(jié)點 
node.master: true 
node.data: true http.port: 9201
transport.tcp.port: 9301# head 插件需要這打開這兩個配置
http.cors.allow-origin: "*"
http.cors.enabled: truecluster.initial_master_nodes: ["node-01", "node-02", "node-03"]
discovery.seed_hosts: ["127.0.0.1:9301", "127.0.0.1:9302", "127.0.0.1:9303"]

節(jié)點2 配置

# 加入如下配置
# 集群名稱
cluster.name: my-application
# 節(jié)點名稱,每個節(jié)點的名稱不能重復(fù) 
node.name: node-02
# 是不是有資格主節(jié)點 
node.master: true 
node.data: true http.port: 9202
transport.tcp.port: 9302# head 插件需要這打開這兩個配置
http.cors.allow-origin: "*"
http.cors.enabled: truecluster.initial_master_nodes: ["node-01", "node-02", "node-03"]
discovery.seed_hosts: ["127.0.0.1:9301", "127.0.0.1:9302", "127.0.0.1:9303"]

節(jié)點3 配置

# 加入如下配置
#集群名稱
cluster.name: my-application
#節(jié)點名稱,每個節(jié)點的名稱不能重復(fù) 
node.name: node-03
#是不是有資格主節(jié)點 
node.master: true 
node.data: true http.port: 9203
transport.tcp.port: 9303# head 插件需要這打開這兩個配置
http.cors.allow-origin: "*"
http.cors.enabled: truecluster.initial_master_nodes: ["node-01", "node-02", "node-03"]
discovery.seed_hosts: ["127.0.0.1:9301", "127.0.0.1:9302", "127.0.0.1:9303"]

注意:

  1. yaml 中數(shù)組第一個元素前必須有空格。
  2. 在啟動 ES 節(jié)點前,將 data 目錄下的數(shù)據(jù)清空。
// http://127.0.0.1:9201/_cluster/health
{"cluster_name": "my-application","status": "green","timed_out": false,"number_of_nodes": 3,"number_of_data_nodes": 3,"active_primary_shards": 1,"active_shards": 2,"relocating_shards": 0,"initializing_shards": 0,"unassigned_shards": 0,"delayed_unassigned_shards": 0,"number_of_pending_tasks": 0,"number_of_in_flight_fetch": 0,"task_max_waiting_in_queue_millis": 0,"active_shards_percent_as_number": 100.0
}http://127.0.0.1:9201/_cat/nodes192.168.3.228 22 79 23 2.61   cdfhilmrstw - node-02
192.168.3.228 19 79 23 2.61   cdfhilmrstw * node-01
192.168.3.228 18 79 17 2.61   cdfhilmrstw - node-03

配置 kibana

配置:config/kibana.yml

# 默認值:http://localhost:9200
elasticsearch.hosts: ["http://localhost:9201", "http://localhost:9202", "http://localhost:9203"]

啟動 kibana

bin/kibana

ES 進階

核心概念

分片

分片:類似數(shù)據(jù)庫中分庫分表的概念。

分片的優(yōu)點

  • 可以水平分割/擴展內(nèi)容容量
  • 在分片之上可以進行分布式并行操作,進而提高性能/吞吐量

一個分片就是一個 Lucene 索引。

副本

副本:分片的備份,類似數(shù)據(jù)庫中的從庫。

副本的優(yōu)點

  • 防止數(shù)據(jù)丟失,提供高可用性。一本主分片和副本不會放在同一個節(jié)點上。
  • 擴展吞吐量,因為搜索可以在所有副本上并行運行。

寫流程

新建,刪除

  1. 客戶端向 Node 1 發(fā)送新建、索引、刪除請求(Node 1 是協(xié)調(diào)節(jié)點)。
  2. Node 1 根據(jù)文檔 _id 計算出屬于 分片 0,通過集群狀態(tài)中的內(nèi)容路由表,獲知分片 0 的主分片位于 Node3,于是將請求轉(zhuǎn)發(fā)給 Node 3。
  3. Node 3 在主分片上執(zhí)行請求(寫請求),如果成功,它轉(zhuǎn)發(fā)到 Node 1 和 Node 2 的副分片上。當(dāng)所有的副節(jié)點報告成功,Node 3 報告成功給協(xié)調(diào)節(jié)點(Node 1),協(xié)調(diào)節(jié)點在報告給客戶端。

路由算法

路由計算公式:shard_num = hash( _routing) % num_primary_shards

默認情況:_routing 值就是文檔 id

這就是為什么主分片數(shù)在創(chuàng)建索引時定義而且不能修改

局部更新

  1. 客戶端向 Node 1 發(fā)送一個更新請求。
  2. 路由到 0 分片上,于是將請求轉(zhuǎn)發(fā)給 Node 3(因為Node 3 有 0 主分片)
  3. Node 3 從主分片上檢索出文檔,修改 _source 字段的 Json,然后在主分片上重建索引。如果有其他進程修改了文檔,它以 retry_on_conflict 設(shè)置的次數(shù)重復(fù)步驟3,都未成功則放棄。
  4. 如果 Node 3 成功更新了文檔,它同時轉(zhuǎn)發(fā)(異步,不保證順序)文檔到 Node1 和 Node 2 上的復(fù)制分片上重建索引。當(dāng)所有復(fù)制節(jié)點報告成功,Node 3 放回成功給請求節(jié)點(Node 1),然后返回給客戶端。

GET 流程

  1. 客戶端向 Node 1 發(fā)送 get 請求。
  2. 路由到 分片 0,分片 0 在 3 個節(jié)點上都有。此時它轉(zhuǎn)發(fā)給 Node 2.
  3. Node 2 返回 endangered 給 Node 1,Node 1 返回給客戶端。

注意:對于讀請求,為了負載平衡,請求節(jié)點( Node1 )會為每一個請求選擇不同的分片(循環(huán)所有分片副本)。

多文檔模式

MGet

  1. 客戶端向 Node 1發(fā)送 mget 請求。
  2. Node 1,為每個分片構(gòu)建一個多條數(shù)據(jù)的檢索,然后轉(zhuǎn)發(fā)這些請求去所需的主分片或者復(fù)制分片上。當(dāng)所有回復(fù)被接收,Node 1 構(gòu)建響應(yīng)并返回給客戶端。

bulk

  1. 客戶端向 Node 1發(fā)送 bulk 請求。
  2. Node 1,為每個分片構(gòu)建批量請求,然后轉(zhuǎn)到這些所需的主分片上。
  3. 主分片順序執(zhí)行操作。當(dāng)一個操作執(zhí)行完畢后,主分片轉(zhuǎn)發(fā)新文檔(或者刪除部分)給對應(yīng)的復(fù)制分片,然后執(zhí)行下一個操作。復(fù)制節(jié)點報告所有操作完成,節(jié)點報告給請求節(jié)點(Node 1),Node 1 構(gòu)建響應(yīng)并返回給客戶端。

Search 流程

query 階段

  1. 客戶端發(fā)送 search 請求到 Node3(協(xié)調(diào)節(jié)點)
  2. Node 3 將請求轉(zhuǎn)發(fā)到索引的每個主分片或者副分片
  3. 每個分片在本地執(zhí)行查詢,并使用本地的 Term/Document Frequency 信息進行打分,添加結(jié)果到大小為 from + size 的本地有序隊列中。
  4. 每個分片返回各自優(yōu)先隊列中所有的文檔 ID 和排序值給協(xié)調(diào)節(jié)點,協(xié)調(diào)節(jié)點合并這些值到自己的優(yōu)先級隊列中,產(chǎn)生一個全局排序后的列表。

注意: 為了避免在協(xié)調(diào)節(jié)點中創(chuàng)建的 number_of_shards * ( from + size ) 優(yōu)先隊列過大,應(yīng)盡量控制分頁深度。

fetch 階段

  1. 協(xié)調(diào)節(jié)點向相關(guān) Node 發(fā)送 MGET 請求。
  2. 分片所在節(jié)點向協(xié)調(diào)節(jié)點返回數(shù)據(jù)。
  3. 協(xié)調(diào)節(jié)點等待所有文檔被取得,然后返回給客戶端。

ES 集成

Spring Data 框架集成

Spring Data 是一個用于簡化數(shù)據(jù)庫、非關(guān)系型數(shù)據(jù)庫、索引庫訪問,并支持云服務(wù)的

開源框架。

Spring Data Elasticsearch

官方網(wǎng)站: https://spring.io/projects/spring-data-elasticsearch

mvn 依賴

    <dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId></dependency></dependencies>

配置文件:application.properties

# es服務(wù)地址
elasticsearch.host=127.0.0.1
# es服務(wù)端口
elasticsearch.port=9200
# 配置日志級別,開啟 debug 日志
logging.level.com.atguigu.es=debug

索引操作

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataIndexTest {// 注入 ElasticsearchRestTemplate@Autowiredprivate ElasticsearchRestTemplate elasticsearchRestTemplate;@Testpublic void createIndex() {// 創(chuàng)建索引,系統(tǒng)會自動化創(chuàng)建索引System.out.println("創(chuàng)建索引");}@Testpublic void deleteIndex() {elasticsearchRestTemplate.deleteIndex(Product.class);}
}

文檔操作

@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESProductDaoTest {@AutowiredProductDao productDao;// 新增@Testpublic void save() {Product product = Product.builder().id(1L).title("華為手機").category("手機").price(9999.0).images("https://xavatar.imedao.com/community/201011/1293612628607-20121221.png!240x240.jpg").build();productDao.save(product);}// 修改@Testpublic void update() {Product product = Product.builder().id(1L).title("小米手機").category("手機").price(9999.0).images("https://xavatar.imedao.com/community/201011/1293612628607-20121221.png!240x240.jpg").build();productDao.save(product);}// 根據(jù) Id 查詢@Testpublic void findById() {Product product = productDao.findById(1L).get();System.out.println(product);}// 查詢?nèi)?/span>@Testpublic void findAll() {Iterable<Product> iterable = productDao.findAll();for (Product product : iterable) {System.out.println(product);}}// 刪除@Testpublic void delete() {Product product = Product.builder().id(1L).build();productDao.delete(product);}// 批量插入@Testpublic void saveAll() {List<Product> productList = new ArrayList<>();for (int i = 0; i < 10; i++) {Product product = Product.builder().id(Long.valueOf(i)).title(i + "小米手機").category("手機").price(9999.0 + i).images("https://xavatar.imedao.com/community/201011/1293612628607-20121221.png!240x240.jpg").build();productList.add(product);}productDao.saveAll(productList);}// 分頁查詢@Testpublic void findByPageable() {Sort sort = Sort.by(Sort.Direction.DESC, "id");int page = 0;int size = 5;PageRequest pageRequest = PageRequest.of(page, size, sort);Page<Product> data = productDao.findAll(pageRequest);for (Product product : data.getContent()) {System.out.println(product);}}
}

文檔檢索

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataEsSearchTest {@AutowiredProductDao productDao;// term 檢索@Testpublic void termQuery(){TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");Iterable<Product> iterable = productDao.search(termQueryBuilder);for (Product product : iterable) {System.out.println(product);}}// term 檢索加分頁@Testpublic void termQueryByPage(){Sort sort = Sort.by(Sort.Direction.DESC, "id");int page = 0;int size = 5;PageRequest pageRequest = PageRequest.of(page, size, sort);TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");Iterable<Product> iterable = productDao.search(termQueryBuilder,pageRequest);for (Product product : iterable) {System.out.println(product);}}
}
http://www.risenshineclean.com/news/59651.html

相關(guān)文章:

  • wordpress 頁面二維碼徐州seo培訓(xùn)
  • wordpress案例分析梅州seo
  • 保險網(wǎng)站有哪些谷歌官方seo入門指南
  • 手機上如何做mv視頻網(wǎng)站網(wǎng)上接單平臺
  • WordPress重力表單注冊石家莊網(wǎng)絡(luò)seo推廣
  • 網(wǎng)站自動秒收錄工具網(wǎng)絡(luò)廣告案例以及分析
  • 佛山網(wǎng)站建設(shè)定制開發(fā)交換友情鏈接的方法
  • 外貿(mào)網(wǎng)站屏蔽國內(nèi)ipseo報名在線咨詢
  • 做淘寶客沒有網(wǎng)站怎么做武漢seo優(yōu)化顧問
  • 個人域名用來做淘寶客網(wǎng)站推介網(wǎng)
  • wap企業(yè)網(wǎng)站模板什么是搜索引擎營銷?
  • 蘇州 網(wǎng)站建設(shè) app移動網(wǎng)站優(yōu)化排名
  • 網(wǎng)站建設(shè)及推廣培訓(xùn)關(guān)鍵詞搜索查找工具
  • 做微信的網(wǎng)站叫什么軟件網(wǎng)站快速上排名方法
  • 做醫(yī)院網(wǎng)站公司推廣app接單網(wǎng)
  • 門源網(wǎng)站建設(shè)公司網(wǎng)絡(luò)廣告形式
  • 網(wǎng)上有做口譯的網(wǎng)站么網(wǎng)站搜索排名優(yōu)化價格
  • 做網(wǎng)站工資年新多少在廣東互聯(lián)網(wǎng)營銷師證書騙局
  • 微應(yīng)用和微網(wǎng)站的區(qū)別鹽城seo優(yōu)化
  • 專業(yè)做網(wǎng)文的網(wǎng)站網(wǎng)站建設(shè)制作專業(yè)
  • 國外建設(shè)工程招聘信息網(wǎng)站怎么寫網(wǎng)站
  • iis如何用ip地址做域名訪問網(wǎng)站汕頭seo快速排名
  • wordpress網(wǎng)站速度時快時慢seo網(wǎng)站優(yōu)化培訓(xùn)廠家報價
  • 如何使用記事本做網(wǎng)站狼雨的seo教程
  • oa報表網(wǎng)站開發(fā)淘寶客推廣一天80單
  • 衡陽商城網(wǎng)站建設(shè)seo刷關(guān)鍵詞排名免費
  • 怎樣在手機做自己的網(wǎng)站企業(yè)培訓(xùn)體系
  • 靈臺縣住房和城鄉(xiāng)建設(shè)局網(wǎng)站品牌推廣方案
  • wordpress子頁面密碼錯誤東營seo
  • 福建建設(shè)執(zhí)業(yè)資格網(wǎng)站報名系統(tǒng)百度推廣后臺登錄入口官網(wǎng)