成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

慕課網(wǎng)_《ElasticSearch入門》學習總結

notebin / 2207人閱讀

摘要:時間年月日星期四說明本文部分內(nèi)容均來自慕課網(wǎng)。那么里面的數(shù)據(jù)就可以分為各種各樣的索引,比如汽車索引圖書索引家具索引等等。圖書索引又可以細分為各種類型,比如科普類小說類技術類等等。具體到每一本書籍,就是文檔,就是整個圖書里面最小的存儲單位。

時間:2017年09月14日星期四
說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com
教學源碼:無
學習源碼:https://github.com/zccodere/s...

第一章:課程介紹 1-1 課程介紹

什么是ElasticSearch

基于Apache Lucene構建的開源搜索引擎
采用Java編寫,提供簡單易用的RESTful API
輕松的橫向擴展,可支持PB級的結構化和非結構化數(shù)據(jù)處理

可用應用場景

海量數(shù)據(jù)分析引擎
站內(nèi)搜索引擎
數(shù)據(jù)倉庫

一線公司實際應用場景

英國衛(wèi)報-實時分析公眾對文章的回應
維基百科、GitHub-站內(nèi)實時搜索
百度-實時日志監(jiān)控平臺
阿里巴巴、谷歌、京東、騰訊、小米等等

前置知識

熟悉用Maven構建項目
了解Spring Boot的基本使用

環(huán)境要求

IDE工具:IntelliJ IDEA、Eclipse等常用IDE即可
Java版本:JDK1.8
其他依賴:Maven、NodeJs(6.0以上)

課程安排

如何安裝單節(jié)點的ElasticSearch
如何安裝插件及插件的主要作用
如何安裝分布式的ElasticSearch
了解ElasticSearch的基礎概念
了解ElasticSearch的基本用法
了解ElasticSearch的高級查詢
使用Spring Boot集合ElasticSearch實戰(zhàn)開發(fā)
第二章:軟件安裝 2-1 版本選擇

ES版本問題

版本歷史:1.x -> 2.x -> 5.x
版本選擇:擁抱新的版本
2-2 單機安裝

學習筆記

單機安裝ElasticSearch

安裝前,請確保已經(jīng)安裝JDK1.8
安裝前,請確保已經(jīng)安裝nodejs6.0以上

官網(wǎng):https://www.elastic.co/products/elasticsearch
下載安裝包:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.0.tar.gz
解壓安裝包:tar -vxf elasticsearch-5.6.0.tar.gz
cd elasticsearch-5.6.0.tar.gz

啟動前,檢查JDK環(huán)境
java -v
請確保已經(jīng)安裝JDK1.8

啟動elasticsearch
sh ./bin/elasticsearch
當日志輸出started時,表示啟動成功

驗證服務
127.0.0.1:9200
elasticsearch服務默認監(jiān)聽9200端口

訪問:http://127.0.0.1:9200
如果出現(xiàn)版本信息,則安裝成功
2-3 插件安裝

學習筆記

實用插件Head安裝

打開github:https://github.com/mobz/elasticsearch-head
下載插件包:https://codeload.github.com/mobz/elasticsearch-head/zip/master
unzip elasticsearch-head-master.zip

cd elasticsearch-head-master

檢查Node環(huán)境
node -v
請確保已經(jīng)安裝nodejs6.0以上

安裝插件
npm install
啟動插件
npm run start
輸出日志表示啟動成功
Started connect web server on http://localhost:9100
訪問
http://localhost:9100


ElasticSearch整合elasticsearch-head插件
cd elasticsearch-5.6.0

vim config/elasticsearch.yml
在配置文件的最后面加上
允許head插件跨域訪問rest接口
http.cors.allowed: true
http.cors.allow-origin: "*"
:wq

后臺啟動
./bin/elasticsearch -d

再次重新啟動elasticsearch-head插件
cd elasticsearch-head-master
啟動插件
npm run start
訪問
http://localhost:9100
2-4 集群安裝

學習筆記

集群安裝
1個master、2個slave

master節(jié)點配置

配置當前節(jié)點為主節(jié)點
cd elasticsearch-5.6.0
修改配置
vim config/elasticsearch.yml
在配置文件的最后面加上

# 指定集群的名字
cluster.name: myes
# 指定當前節(jié)點的名字
node.name: master
# 指定當前節(jié)點為master
node.master: true
# 指定綁定的IP
network.host: 127.0.0.1
# 使用默認端口:9200
:wq

ps -ef | grep "pwd"

kill pid
重新啟動
./bin/elasticsearch -d
檢查服務是否正常啟動
http://localhost:9200

slave節(jié)點配置

mkdir es_slave
cp elasticsearch-5.6.0.tar.gz es_slave/
cd es_slave
tar -vxf elasticsearch-5.6.0.tar.gz
cp -r elasticsearch-5.6.0 es_slave1
cp -r elasticsearch-5.6.0 es_slave2

修改es_slave1配置
cd es_slave1
vim config/elasticsearch.yml
在配置文件的最后面加上

# 指定集群的名字:需要和master節(jié)點一致
cluster.name: myes
# 指定當前節(jié)點的名字
node.name: slave1
# 指定綁定的IP
network.host: 127.0.0.1
# 指定當前節(jié)點綁定端口號8200
http.port: 8200
# 該配置主要是為了找到master節(jié)點
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
:wq

啟動服務
./bin/elasticsearch -d
檢查服務是否正常啟動
http://localhost:9100

安裝之前的步驟配置slave2
cd es_slave2
vim config/elasticsearch.yml
在配置文件的最后面加上

# 指定集群的名字:需要和master節(jié)點一致
cluster.name: myes
# 指定當前節(jié)點的名字
node.name: slave2
# 指定綁定的IP
network.host: 127.0.0.1
# 指定當前節(jié)點綁定端口號8000
http.port: 8000
# 該配置主要是為了找到master節(jié)點
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
:wq

啟動服務
./bin/elasticsearch -d
檢查服務是否正常啟動
http://localhost:9100
第三章:基礎概念 3-1 基礎概念

集群和節(jié)點

一個集群是由一個或多個ES組成的集合
每一個集群都有一個唯一的名字
每一個節(jié)點都是通過集群的名字來加入集群的
每一個節(jié)點都有自己的名字
節(jié)點能夠存儲數(shù)據(jù),參與集群索引數(shù)據(jù)以及搜索數(shù)據(jù)的獨立服務

基礎概念

索引:含有相同屬性的文檔集合
類型:索引可以定義一個或多個類型,文檔必須屬于一個類型
(通常會定義有相同字段的文檔作為一個類型)
文檔:文檔是可以被索引的基本數(shù)據(jù)單位

三者之間的關系

索引相當于SQL里的DataBase,也就是數(shù)據(jù)庫
類型相當于SQL里的Table,也就是表
文檔相當于SQL里的一行記錄,也就是一行數(shù)據(jù)

舉個例子

假設有一個信息查詢系統(tǒng),使用ES做存儲。那么里面的數(shù)據(jù)就可以分為各種各樣的索引,比如:汽車索引、圖書索引、家具索引等等。圖書索引又可以細分為各種類型,比如:科普類、小說類、技術類等等。具體到每一本書籍,就是文檔,就是整個圖書里面最小的存儲單位。

和索引相關的兩個高級概念

分片:每個索引都有多個分片,每個分片是一個Lucene索引
備份:拷貝一份分片就完成了分片的備份
ES默認在創(chuàng)建索引時,會創(chuàng)建5個分片、1個備份
分片的數(shù)量只能在創(chuàng)建索引時設置,而不能在后期進行修改
備份是可以動態(tài)修改的
第四章:基本用法 4-1 創(chuàng)建索引

ES的API組成結構:使用RESTful API風格來命名API

API基本格式:http://:/<索引>/<類型>/<文檔id>
常用HTTP動詞:GET/PUT/POST/DELETE

使用Head插件創(chuàng)建非結構化索引

訪問:localhost:9100
路徑:索引->新建索引->索引名稱:book->點擊OK
索引名稱:必須小寫,不能有中劃線

如何區(qū)分索引是結構化的還是非結構化的

結合Head插件查看
點擊索引信息->索引信息->mappings節(jié)點
當mappings節(jié)點后的內(nèi)容為空時:非結構化索引

使用Head插件創(chuàng)建結構化索引

路徑:復合查詢->查詢->book/novel/_mappings
指定映射:使用JSON結構體
{
"novel":{
"propertise":{
  "title":{"type":"test"}
   }
}
}
然后勾選易讀->點擊驗證JSON->提交請求
再次查看mappings節(jié)點時,已經(jīng)不是空的了

使用PostMan創(chuàng)建索引

PUT:127.0.0.1:9200/people
Body->raw->JSON(application/json)
編寫JSON體
點擊Send,然后到Head插件中查看people索引信息

編寫JSON體如下

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "man":{
            "properties":{
                "name":{
                    "type": "text"
                },
                "country":{
                    "type": "keyword"
                },
                "age":{
                    "type": "integer"
                },
                "date":{
                    "type": "date",
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        },
        "woman":{
            
        }
    }
    
}
4-2 新增文檔

新增文檔

指定文檔id新增
自動產(chǎn)生文檔id新增
文檔id:唯一索引值指向文檔數(shù)據(jù)

使用PostMan工具新增數(shù)據(jù)-指定文檔id新增

PUT:127.0.0.1::9200/people/man/1
Body->raw->JSON(application/json)
{
"name":"zc",
"country":"china",
"age":22,
"date":"1995-01-01"
}
點擊Send,可以看到ES響應的信息
使用Head插件查看索引下的數(shù)據(jù),docs字段代表索引下所有文檔的數(shù)量值
點擊數(shù)據(jù)瀏覽,可以看見剛剛新增的數(shù)據(jù)

使用PostMan工具新增數(shù)據(jù)-自動產(chǎn)生文檔id新增

POST:127.0.0.1::9200/people/man/
Body->raw->JSON(application/json)
{
"name":"myzc",
"country":"china",
"age":22,
"date":"1995-02-01"
}
點擊Send,可以看到ES響應的信息
使用Head插件,點擊數(shù)據(jù)瀏覽,可以看見剛剛新增的數(shù)據(jù)
4-3 修改文檔

修改文檔

直接修改文檔
腳本修改文檔

使用PostMan工具修改文檔-指定文檔ID修改

POST:127.0.0.1:9200/people/man/1/_update
Body->raw->JSON(application/json)
{
"doc":{
    "name":"who is zc"
}
}
點擊Send,可以看到ES響應的信息
使用Head插件,點擊數(shù)據(jù)瀏覽,可以看見剛剛修改的數(shù)據(jù)

使用PostMan工具修改文檔-指定文檔ID使用腳本修改

POST:127.0.0.1:9200/people/man/1/_update
Body->raw->JSON(application/json)
{
"script":{
    "lang":"painless",
    "inline":"ctx._sources.age+=10"
}
}
或使用以下格式
{
"script":{
    "lang":"painless",
    "inline":"ctx._sources.age = params.age",
    "params":{
        "age":100
    }
}
}
點擊Send,可以看到ES響應的信息
使用Head插件,點擊數(shù)據(jù)瀏覽,可以看見剛剛修改的數(shù)據(jù)
4-4 刪除文檔

刪除

刪除文檔
刪除索引

使用PostMan刪除文檔-指定文檔ID

DELETE:127.0.0.1:9200/people/man/1
點擊Send,可以看到ES響應的信息
使用Head插件,點擊數(shù)據(jù)瀏覽,可以看見數(shù)據(jù)已經(jīng)刪除

使用Head插件刪除索引

路徑:概覽->book->動作->刪除->輸入刪除->確定
注意:刪除操作本身很危險,刪除索引時會刪除它所有的文檔數(shù)據(jù)

使用PostMan刪除索引

DELETE:127.0.0.1:9200/people
點擊Send,可以看到ES響應的信息
使用Head插件,點擊數(shù)據(jù)瀏覽,可以看見索引已經(jīng)刪除
4-5 查詢語法

ES查詢分類

簡單查詢
條件查詢
聚合查詢

前置條件,創(chuàng)建book索引,并預先新增一些數(shù)據(jù)

使用PostMan簡單查詢-指定文檔ID

GET:127.0.0.1:9200/book/novel/1
點擊Send,可以看到ES響應的信息

使用PostMan條件查詢

POST:127.0.0.1:9200/book/_search
Body->raw->JSON(application/json)
編寫查詢JSON體
點擊Send,可以看到ES響應的信息

編寫查詢JSON體如下

查詢所有數(shù)據(jù)

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

用from指定從哪里返回,用size指定返回的數(shù)據(jù)大小

{
    "query":{
        "match_all":{}
    },
    "from":1,
    "size":1
}

使用關鍵字查詢,查詢標題含有ElasticSearch的數(shù)據(jù)

{
    "query":{
        "match":{
            "title":"ElasticSearch"
        }
    }
}

使用sort指定結果集排序-按照出版日期倒序

{
    "query":{
        "match":{
            "title":"ElasticSearch"
        }
    },
    "sort":[
        {
            "publish_date":{
                "order":"desc"
            }
        }    
    ]
}

按照書籍的字數(shù)進行單個聚合查詢

{
    "aggs":{
        "group_by_word_count":{
            "terms":{
                "field":"word_count"
            }
        }
    }
}

按照書籍的字數(shù)及出版日期進行多個聚合查詢

{
    "aggs":{
        "group_by_word_count":{
            "terms":{
                "field":"word_count"
            }
        },
        "group_by_publish_date":{
            "terms":{
                "field":"publish_date"
            }
        }
    }
}

對書籍字數(shù)進行統(tǒng)計計算

{
    "aggs":{
        "grades_word_count":{
            "stats":{
                "field":"word_count"
            }    
        }
    }
}
第五章:高級查詢 5-1 query語法

高級查詢

子條件查詢:特定字段查詢所指特定值
query context
filter context
復合條件查詢:以一定的邏輯組合子條件查詢
固定分數(shù)查詢
布爾查詢

query context介紹

在查詢過程中,除了判斷是否滿足查詢條件外
ES還會計算一個_score來標識匹配的程度
旨在判斷目標文檔和查詢條件匹配的有多好

query context查詢

全文本查詢:針對文本類型數(shù)據(jù)
字段級別查詢:針對結構化數(shù)據(jù),如數(shù)字、日期等

使用PostMan進行query context文本查詢

POST:127.0.0.1:9200/book/_search
Body->raw->JSON(application/json)
編寫查詢JSON體
點擊Send,可以看到ES響應的信息

編寫查詢JSON體如下

使用match關鍵字模糊匹配

{
    "query":{
        "match":{
            "author":"wali"
        }
    }
}

使用match_phrase關鍵字習語匹配

{
    "query":{
        "match_phrase":{
            "author":"ElasticSearch入門"
        }
    }
}

使用multi_match查詢作者和標題包含wali的數(shù)據(jù)

{
    "query":{
        "multi_match":{
            "query":"wali",
            "fields":["author","title"]
        }
    }
}

使用query_string進行語法查詢

{
    "query":{
        "query_string":{
            "query":"(ElasticSearch AND 大法) OR Python"
        }
    }
}

使用query_string查詢多個字段

{
    "query":{
        "query_string":{
            "query":"wali OR ElasticSearch",
            "field":["title","author"]
        }
    }
}

使用PostMan進行query context字段查詢

POST:127.0.0.1:9200/book/_search
Body->raw->JSON(application/json)
編寫查詢JSON體
點擊Send,可以看到ES響應的信息

編寫查詢JSON體如下

查詢字數(shù)在某個特定集(1000)的書籍

{
    "query":{
        "term":{
            "word_count":1000
        }
    }
}

查詢字符在某個范圍(大于等于1000-小于等于2000)的書籍

{
    "query":{
        "range":{
            "word_count":{
                "gte":1000,
                "lte":2000
            }
        }
    }
}

查詢出版日期在某個范圍(2017-01-01至2017-12-31)的書籍

{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2017-01-01",
                "lte":"2017-12-31"http://或 "lte":"now"
            }
        }
    }
}

使用關鍵now,代指當前日志(即現(xiàn)在)

5-2 filter語法

filter context介紹

在查詢過程中,只判斷該文檔是否滿足條件
只有Yes或No

使用PostMan進行filter context查詢

POST:127.0.0.1:9200/book/_search
Body->raw->JSON(application/json)
點擊Send,可以看到ES響應的信息

查詢字數(shù)1000的書籍

{
    "query":{
        "bool":{
            "filter":{
                "term":{
                    "word_count":1000
                }
            }
        }
    }
}
5-3 復合查詢

常用復合條件查詢

固定分數(shù)查詢
布爾查詢

使用PostMan進行復合查詢

127.0.0.1:9200 /_search
Body->raw->JSON(application/json)
點擊Send,可以看到ES響應的信息

全文搜索-標題含有ElasticSearch的書籍

{
    "query":{
        "constant_score":{
            "filter":{
                "match":{
                    "title": "ElasticSearch"
                }
            },
            "boost":2
        }
    }
}

布爾查詢- should滿足任意條件

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "author":"wali"
                    }
                },
                {
                    "match":{
                        "title":"ElasticSearch"
                    }
                }
            ]
        }
    }
}

布爾查詢- must滿足全部條件

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "author":"wali"
                    }
                },
                {
                    "match":{
                        "title":"ElasticSearch"
                    }
                }
            ]
        }
    }
}

使用must和filter復合查詢

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "author":"wali"
                    }
                },
                {
                    "match":{
                        "title":"ElasticSearch"
                    }
                }
            ],
            "filter":[
                {
                    "term":{
                        "word_count":1000
                    }
                }
            ]
        }
    }
}

布爾查詢- must_not一定不能滿足的條件

{
    "query":{
        "bool":{
            "must_not":{
                "term":{
                    "author":"wali"
                }
            }
        }
    }
}
第六章:實戰(zhàn)開發(fā) 6-1 環(huán)境搭建

實戰(zhàn)演練

SpringBoot集成ES
圖書信息管理接口開發(fā)

創(chuàng)建名為springbootes的gradle項目build.gradle如下

buildscript {
    ext {
        springBootVersion = "1.5.6.RELEASE"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "org.springframework.boot"

group = "com.myimooc"
version = "0.0.1-SNAPSHOT"
sourceCompatibility = 1.8

repositories {
    maven{url:"http://maven.aliyun.com/nexus/content/groups/public/"}
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    //compile("org.springframework.boot:spring-boot-starter-data-elasticsearch")
    
    compile("org.elasticsearch.client:transport:5.5.2")
    compile("org.apache.logging.log4j:log4j-core:2.7.0")
    
    testCompile("org.springframework.boot:spring-boot-starter-test")
}
6-2 接口開發(fā)

接口列表

新增圖書信息
修改圖書信息
刪除圖書信息
綜合查詢功能

代碼編寫

1.編寫EsConfig類

package com.myimooc.springbootes.config;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @title ElasticSearch配置類
 * @describe ElasticSearch配置
 * @author zc
 * @version 1.0 2017-09-15
 */
@Configuration
public class EsConfig {
    
    @Bean
    public TransportClient client() throws UnknownHostException{
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);
        
        Settings settings = Settings.builder()
                // es集群名稱
                .put("cluster.name", "myes")
                .build();
        
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        
        return client;
    }
    
}

2.編寫B(tài)ookRest類

package com.myimooc.springbootes.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @title 圖書REST接口類
 * @describe 調(diào)ES接口
 * @author zc
 * @version 1.0 2017-09-15
 */
@RestController
public class BookRest {
    
    @Autowired
    private TransportClient client;
    
    @GetMapping("/")
    public String index(){
        return "index";
    }
    
    /**
     * @describe 查詢接口
     * @author zc
     * @version 1.0 2017-09-15
     */
    @GetMapping("/get/book/novel")
    public ResponseEntity get(@RequestParam(name="id",defaultValue="")String id){
        
        if(id.isEmpty()){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        
        GetResponse result = this.client.prepareGet("book","novel",id).get();
        
        if(!result.isExists()){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        
        return new ResponseEntity<>(result.getSource(), HttpStatus.OK);
    }
    
    /**
     * @describe 增加接口
     * @author zc
     * @version 1.0 2017-09-15
     */
    @PostMapping("/add/book/novel")
    public ResponseEntity add(
            @RequestParam(name="title")String title,
            @RequestParam(name="author")String author,
            @RequestParam(name="word_count")int wordCount,
            @RequestParam(name="publish_date") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date publishDate){
        
        try {
            XContentBuilder content = XContentFactory.jsonBuilder()
                .startObject()
                .field("title",title)
                .field("author", author)
                .field("word_count", wordCount)
                .field("publish_date", publishDate.getTime())
                .endObject();
            IndexResponse result = this.client.prepareIndex("book","novel").setSource(content).get();
            return new ResponseEntity<>(result.getId(),HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    /**
     * @describe 刪除接口
     * @author zc
     * @version 1.0 2017-09-15
     */
    @DeleteMapping("/delete/book/novel")
    public ResponseEntity delete(@RequestParam(name="id",defaultValue="")String id){
        DeleteResponse result = this.client.prepareDelete("book", "novel", id).get();
        return new ResponseEntity<>(result.toString(),HttpStatus.OK);
    }
    
    /**
     * @describe 修改接口
     * @author zc
     * @version 1.0 2017-09-15
     */
    @DeleteMapping("/update/book/novel")
    public ResponseEntity update(
            @RequestParam(name="id",defaultValue="")String id,
            @RequestParam(name="title",required=false)String title,
            @RequestParam(name="author",required=false)String author){
        
        UpdateRequest update = new UpdateRequest("book","novel",id);
        try {
            XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject();
            if(!StringUtils.isEmpty(title)){
                builder.field("title",title);
            }
            if(!StringUtils.isEmpty(author)){
                builder.field("author", author);
            }
            builder.endObject();
            update.doc(builder);
            UpdateResponse result = this.client.update(update).get();
            return new ResponseEntity<>(result.toString(),HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    /**
     * @describe 復合查詢
     * @author zc
     * @version 1.0 2017-09-15
     */
    @DeleteMapping("/query/book/novel")
    public ResponseEntity query(
            @RequestParam(name="author",required=false)String author,
            @RequestParam(name="title",required=false)String title,
            @RequestParam(name="gt_word_count",defaultValue="0") int gtWordCount,
            @RequestParam(name="lt_word_count",required=false) Integer ltWordCount){
        
        // 構建布爾查詢
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if(!StringUtils.isEmpty(author)){
            boolQuery.must(QueryBuilders.matchQuery("author", author));
        }
        if(!StringUtils.isEmpty(title)){
            boolQuery.must(QueryBuilders.matchQuery("title", title));
        }
        
        // 構建范圍查詢
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("word_count")
            .from(gtWordCount);
        if(ltWordCount != null && ltWordCount > 0){
            rangeQuery.to(ltWordCount);
        }
        
        // 使用filter構建
        boolQuery.filter(rangeQuery);
        
        SearchRequestBuilder builder = this.client.prepareSearch("book")
            .setTypes("novel")
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(boolQuery)
            .setFrom(0)
            .setSize(10);
        
        System.out.println("[ES查詢請求參數(shù)]:"+builder);
        
        SearchResponse response = builder.get();
        
        List> result = new ArrayList>();
        
        for(SearchHit hit:response.getHits()){
            result.add(hit.getSource());
        }
        
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
}

3.編寫SpringbootesApplication類

package com.myimooc.springbootes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @title SpringBoot集成ElasticSearch
 * @describe 啟動類
 * @author zc
 * @version 1.0 2017-09-15
 */
@SpringBootApplication
public class SpringbootesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootesApplication.class, args);
    }
    
}
第七章:課程總結 7-1 課程總結

課程總結

ES簡介:使用場景例子、ES的重要性
ES安裝:單機安裝、集群安裝、Head插件安裝
ES基礎:核心基礎概念:索引、類型、文檔
ES用法:基本用法:增刪改查
ES高級:高級查詢語法
ES實戰(zhàn):SpringBoot集成ES開發(fā)增刪改查接口

文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/70474.html

相關文章

  • 課網(wǎng)_《RxJava與RxAndroid基礎入門學習總結

    時間:2017年10月16日星期一說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學源碼:無學習源碼:https://github.com/zccodere/s... 第一章:課程簡介 1-1 課程介紹 本門課程的主要內(nèi)容 RxJava是什么 RxAndroid是什么 RxJava常用操作符(重點、難點) 怎樣在項目中使用RxJava和RxAndroid 如何學...

    劉明 評論0 收藏0
  • 課網(wǎng)_《SpringMVC數(shù)據(jù)綁定入門學習總結

    摘要:數(shù)據(jù)綁定入門學習總結時間年月日星期日說明本文部分內(nèi)容均來自慕課網(wǎng)。慕課網(wǎng)教學示例源碼個人學習源碼第一章課程介紹數(shù)據(jù)綁定入門概述數(shù)據(jù)綁定概念來自百度百科簡單綁定是將一個用戶界面元素控件的屬性綁定到一個類型對象實例上的某個屬性的方法。 《SpringMVC數(shù)據(jù)綁定入門》學習總結 時間:2017年2月19日星期日說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.co...

    Karrdy 評論0 收藏0
  • 課網(wǎng)_《Spring入門篇》學習總結

    摘要:入門篇學習總結時間年月日星期三說明本文部分內(nèi)容均來自慕課網(wǎng)。主要的功能是日志記錄,性能統(tǒng)計,安全控制,事務處理,異常處理等等。 《Spring入門篇》學習總結 時間:2017年1月18日星期三說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學示例源碼:https://github.com/zccodere/s...個人學習源碼:https://git...

    Ververica 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<