(轉)ElasticSearch學習
阿新 • • 發佈:2018-12-10
(二期)21、全文搜尋引擎Elasticsearch
Document
Field
Analyzer
IndexWriter
Directory
Query
Term
TermQuery
IndexSearcher
Hits
索引過程
搜尋過程
cd elasticsearch-6.4.3
#啟動
./bin/elasticsearch
- 單例安裝
#新增一個使用者
useradd lfq
#授權
chown -R lfq /opt/elasticsearch-*
#切換到這個使用者
su lfq
su root
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
su root
vim /etc/sysctl.conf
vm.max_map_count=655360
sysctl -p
cat config/jvm.options
-Xms1g 改成 -Xms512m
vim config/elasticsearch.yml
#新增下面一行即可
network.host: 0.0.0.0
#允許跨域訪問,方便後面視覺化工具elasticsearch-head
http.cors.enabled: true
http.cors.allow-origin: "*"
- 分散式安裝
主節點配置
vim config/elasticsearch.yml
#新增一下配置
cluster.name: kobe
node.name: master
node.master: true
從節點1
vim config/elasticsearch.yml
cluster.name: kobe
node.name: slave1
http.port: 9800
network.host: 0.0.0.0
#設定主動發現主節點地址
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
- elasticsearch-head安裝
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install 或者cnpm install(需要安裝淘寶映象)
npm run start
開啟瀏覽器 http://localhost:9100/
檢查叢集狀態
curl localhost:9200/_cat/health?v
curl localhost:9200/_cat/nodes?v
curl localhost:9200/_cat/indices?v
適合舊版本的,新版本看postman
curl -XPUT localhost:9200/索引名/型別/id -d {//JSON格式的body體}
curl -XDELETE localhost:9200/索引名
curl -XGET localhost:9200/索引名/型別/id
#其中 q= 表示匹配索引中指定資料
curl -XGET localhost:9200/索引名/_search?q=
_bulk 批量操作
_search 搜尋
?pretty 表示格式化輸出
* 使用QueryBuilder
* termQuery("key", obj) 完全匹配
* termsQuery("key", obj1, obj2..) 一次匹配多個值
* matchQuery("key", Obj) 單個匹配, field不支援萬用字元, 字首具高階特性
* multiMatchQuery("text", "field1", "field2"..); 匹配多個欄位, field有萬用字元忒行
* matchAllQuery(); 匹配所有檔案
* 組合查詢
* must(QueryBuilders) : AND
* mustNot(QueryBuilders): NOT
* should: : OR
* 只查詢一個id的
* QueryBuilders.idsQuery(String...type).ids(Collection<String> ids)
/**
* moreLikeThisQuery: 實現基於內容推薦, 支援實現一句話相似文章查詢
* {
"more_like_this" : {
"fields" : ["title", "content"], // 要匹配的欄位, 不填預設_all
"like_text" : "text like this one", // 匹配的文字
}
}
percent_terms_to_match:匹配項(term)的百分比,預設是0.3
min_term_freq:一篇文件中一個詞語至少出現次數,小於這個值的詞將被忽略,預設是2
max_query_terms:一條查詢語句中允許最多查詢詞語的個數,預設是25
stop_words:設定停止詞,匹配時會忽略停止詞
min_doc_freq:一個詞語最少在多少篇文件中出現,小於這個值的詞會將被忽略,預設是無限制
max_doc_freq:一個詞語最多在多少篇文件中出現,大於這個值的詞會將被忽略,預設是無限制
min_word_len:最小的詞語長度,預設是0
max_word_len:最多的詞語長度,預設無限制
boost_terms:設定詞語權重,預設是1
boost:設定查詢權重,預設是1
analyzer:設定使用的分詞器,預設是使用該欄位指定的分詞器
*/
/**
* 字首查詢
*/
@Test
public void testPrefixQuery() {
QueryBuilder queryBuilder =