Elasticsearch 的安裝和基本使用
標簽: es
Elasticsearch 是一個搜索服務器,特點:分布式、易于擴展、全文檢索、索引速度快。
本篇文章主要介紹 Elasticsearch 的安裝和基本使用,假定你有一定的Linux基礎(所有命令均在命令行中執行)。
Elasticsearch 版本:2.2.0 csdn下載
服務器:CentOS 6.4 (win7 下的虛擬機)
一、安裝
因為 Elasticsearch 是 Java 開發的,所以要先安裝 Java(下載)
可用 java -version來查 看是否已安裝Java
若沒有安裝,且jdk 在 /usr/local/src/jdk-7u79-linux-x64.gz 目錄下
tar zxvf /usr/local/src/jdk-7u79-linux-x64.gz cp -r /usr/local/src/jdk1.7.0_79/ /usr/local/java
添加 java 環境變量,保存并退出
vim /etc/profile #在最后面添加 export JAVA_HOME=/usr/local/java export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
執行 source /etc/profile 使更改生效,再使用 java -version 看一下java 版本
安裝 Elasticsearch ,假定安裝包在 /usr/local/src/elasticsearch-2.2.0.tar.gz 目錄下
tar -zxvf elasticsearch-2.2.0.tar.gz -C /usr/local
這樣在 /usr/local 下就有一個 elasticsearch-2.2.0 的目錄
Elasticsearch 的配置文件在 /usr/local/elasticsearch-2.2.0/config/elasticsearch.yml
打開配置文件,加入如下配置(可以不用配置)
#集群名稱,若有多臺服務器 cluster.name: cluster-test #節點名稱,本服務器的名稱 node.name: node-136 #監聽端口,默認為 9200 http.port: 9200
打開端口,Elasticsearch 默認監聽9200 端口,可在 elasticsearch.yml 中修改
#打開 iptables vim /etc/sysconfig/iptables #加入 -A INPUT -p tcp -m state --state NEW -m tcp --dport 9200 -j ACCEPT -A INPUT -p udp -m state --state NEW -m udp --dport 9200 -j ACCEPT #重啟生效 service iptables restart
啟動 Elasticsearch。為安全考慮,Elasticsearch不允許 root 啟動,所以你要先創建一個用于啟動 Elasticsearch 的用戶,并將 elasticsearch-2.2.0 文件的所有者賦予該用戶
假如你已經創建了一個 elastic 的用戶(我創建的用戶名是 jam)
chown -R elastic:elastic /usr/local/elasticsearch-2.2.0/
啟動(-d 表示后臺運行)
/usr/local/elasticsearch-2.2.0/bin/elasticsearch -d
查看是否啟動
ps aux |grep elastic #有如下信息表示成功 jam 44292 3.4 17.4 2088296 176320 pts/1 Sl 17:37 0:05 /usr/bin/java -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/local/elasticsearch-2.2.0 -cp /usr/local/elasticsearch-2.2.0/lib/elasticsearch-2.2.0.jar:/usr/local/elasticsearch-2.2.0/lib/* org.elasticsearch.bootstrap.Elasticsearch start -d
停止 Elasticsearch
# 40952 pid,就是上面信息中的 第二個數據 kill -9 44292
二、使用
1、基本概念
傳統關系型數據庫(如 MySQL)與 Elasticsearch 對比
Relational DB | Elasticsearch | 釋義 |
---|---|---|
Databases | Indices | 索引(名詞)即數據庫 |
Tables | Types | 類型即表名 |
Rows | Documents | 文檔即每行數據 |
Columns | Fields | 字段 |
2、與 Elasticsearch 通信
Elasticsearch 支持 RESTful API的訪問方式,這里我們使用 curl 訪問和操作Elasticsearch
官方也有很多對應語言的 API 供大家選擇
PHP-API
JAVA-API
Python-API
初探:查看 Elasticsearch 的基本信息
#pretty 參數使返回的結果格式化更易讀 curl 'http://localhost:9200/?pretty' #返回 { "name" : "node-136","cluster_name" : "cluster-test", "version" : { "number" : "2.2.0", "build_hash" : "8ff36d139e16f8720f2947ef62c8167a888992fe", "build_timestamp" : "2016-01-27T13:32:39Z", "build_snapshot" : false, "lucene_version" : "5.4.1" }, "tagline" : "You Know, for Search" }
簡單介紹一下 curl 命令
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>' VERB HTTP方法:GET, POST, PUT, HEAD, DELETE PROTOCOL http或者https協議(只有在Elasticsearch前面有https代理的時候可用) HOST Elasticsearch集群中的任何一個節點的主機名 PORT Elasticsearch HTTP服務所在的端口,默認為9200 PATH API路徑,資源路徑(例如_count將返回集群中文檔的數量) QUERY_STRING 一些可選的查詢請求參數,例如?pretty參數將返回易讀的JSON數據 BODY 一個JSON格式的請求主體(如果請求需要的話)
3、elasticsearch-head 插件
這里介紹一個 Elasticsearch 可視化的管理插件 elasticsearch-head,可方便的查看,刪除,管理數據(生產環境不推薦安裝)
root 安裝
cd /usr/local/elasticsearch-2.2.0/bin/ ./plugin install mobz/elasticsearch-head
訪問(linux 有桌面系統帶瀏覽器的可直接訪問):http://127.0.0.1:9200/_plugin/head/
那外網如何訪問呢,使用 Nginx 代理,修改Nginx 配置文件加入
server { listen 8080; #192.168.1.136 是我虛擬機的 ip 地址 server_name 192.168.1.136; location / { #http://127.0.0.1:9200/ 是后端代理的地址 proxy_pass http://127.0.0.1:9200/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
重啟nginx,在windows下訪問 http://192.168.1.136:8080/_plugin/head/,界面如下
可以查看集群信息,節點信息,索引信息…都是以json 形式顯示這些信息
4、創建、查看索引
現在我們來添加一個名叫 test 的索引(可理解為數據庫)
curl -XPUT 'http://localhost:9200/test?pretty' #返回 { "acknowledged" : true }
創建成功后,默認會分配五個主分片(創建后不可更改,通過算法將數據存放在這五個分片中的一個,增刪改都是針對主分片的)和一個復制分片(可修改,每個主分片都對應一個復制分片),這兩個默認值都可以在配置文件中修改,也可以在創建的時候指定,如
curl -XPUT 'http://localhost:9200/test?pretty' -d '{ "settings": { "number_of_shards" : 2, #2個主分片"number_of_replicas" : 0 #0個復制分片 } }'
查看索引
curl -XGET 'http://localhost:9200/test?pretty'
5、創建、查看 類型
創建一個名叫 article 的類型(即表名),有這幾個字段,id、subject、content、author
curl -XPUT 'http://localhost:9200/test/_mapping/article?pretty' -d '{ "properties": { "id": { "type": "integer", "index": "not_analyzed" }, "subject": { "type": "string", "analyzer": "standard" }, "content": { "type": "string", "analyzer": "standard" }, "author": { "type": "string", "index": "not_analyzed" } } }'
type:是指定字段的數據類型
index:有三個選項 analyzed(當成全文來搜索),not_analyzed(當成一個準確的值),no(完全不可被搜索)
analyzer:索引和搜索時全文字段(即此字段)使用的分析器,standard 為 Elasticsearch 默認的全文字段分析器(對中文不友好,可使用 ik 代替)
Elasticsearch 字段類型和對應的分類類型
分類類型 | 數據類型 |
---|---|
String | string |
Whole number | byte , short , integer , long |
Floating point | float , double |
Boolean | boolean |
Date | date |
查看剛才新增的類型
curl -XGET 'http://localhost:9200/test/article/_mapping/?pretty' #返回 { "test" : { "mappings" : { "article" : { "properties" : { "author" : { "type" : "string", "index" : "not_analyzed" }, "content" : { "type" : "string", "analyzer" : "standard" }, "id" : { "type" : "integer" }, "subject" : { "type" : "string", "analyzer" : "standard" } } } } } }
6、創建、查看、修改、刪除 文檔
創建一條數據
指定 _id 的創建
curl -XPUT 'http://localhost:9200/test/article/1?pretty' -d '{ "id": 1, "subject": "第一篇文章標題","content": "第一篇文章內容", "author": "jam" }' #返回 { #文檔所在的索引 "_index" : "test", #文檔的類型名"_type" : "article", #文檔的字符串 ID,在請求鏈接中指定的1,若不指定,會自動生成 "_id" : "1", #文檔版本號,修改或刪除都會增加 "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, #表示創建成功 "created" : true }
自增 _id 的創建
curl -XPOST 'http://localhost:9200/test/article?pretty' -d '{ "id": 2, "subject": "第二篇文章標題", "content": "第二篇文章內容", "author": "jam" }' #返回 ... "_id" : "AVf_6fM1vEkwGPLuUJqp", ...
查看指定文檔
curl -XGET 'http://localhost:9200/test/article/1?pretty' #返回 { "_index" : "test", "_type" : "article", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "id" : 1, "subject" : "第一篇文章標題","content" : "第一篇文章內容", "author" : "jam" } }
查看所有文檔(_search)
curl -XGET 'http://localhost:9200/test/article/_search?pretty'
更新文檔(_update),局部更新(更新指定字段)
curl -XPOST 'http://localhost:9200/test/article/1/_update?pretty' -d ' { "doc" : { "content" : "第一篇文章內容-更新后" } }' #返回 { "_index" : "test", "_type" : "article", "_id" : "1", "_version" : 2, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }
_version 版本號變為 2
刪除文檔
curl -XDELETE 'http://localhost:9200/test/article/1?pretty' #返回 { "found" : true, "_index" : "test", "_type" : "article", "_id" : "1", "_version" : 3, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 } }
_version 版本號變為 3,說明刪除是不會真正刪除文檔的
7、批量操作——bulk API 介紹
先看一個批量創建文檔的例子
curl -XPOST 'http://localhost:9200/test/article/_bulk?pretty' -d ' { "index" : { "_id" : "3" } } { "id" : 3,"subject" : "第三篇文章標題","content" : "第三篇文章內容" ,"author": "jam"} { "index" : { "_id": "4" } } { "id" : 4,"subject" : "第四篇文章標題","content" : "第四篇文章內容" ,"author": "tomi"} ' #返回 { "took" : 36, "errors" : false, "items" : [ { "index" : { "_index" : "test", "_type" : "article","_id" : "3", "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "status" : 201 } }, { "index" : { "_index" : "test", "_type" : "article", "_id" : "4", "_version" : 1, "_shards": { "total" : 2, "successful" : 1, "failed" : 0 }, "status" : 201 } } ] }
bulk API允許我們使用單一請求來實現多個文檔的 create(文檔不存在時才創建) 、 index(創建新文檔或替換已有文檔) 、 update 或 delete
它的請求體格式如下(也就是 -d 后面的數據)
{ action: { metadata }}\n { request body }\n { action: { metadata }}\n { request body }\n
action 有四個選項,即 create 、 index 、 update 、 delete
metadata 為元數據,即是 _index 、_type、_id,上面的例子中,因為我在請求鏈接中指定了 _index (test)和_type(article),所以我只需指定 _id
request body 這里就是每條數據的具體內容,字段名和數據一一對應就可以了
需要注意的是每行必須以 “\n” 符號結尾, 包括最后一行。直接敲回車鍵就可以了
可以在一條 bulk 的請求體中既執行 index(創建文檔),又執行 update(更新文檔),還可以執行 delete(刪除文檔),請求體類似如下
{ "delete": { "_index": "test", "_type": "article", "_id": "1" }} { "create": { "_index": "test", "_type": "article", "_id": "123" }} { "id" : 123,"subject" : "第123篇文章標題","content" : "第123篇文章內容" ,"author": "jam123"} { "index": { "_index": "test", "_type": "article", "_id": "3" }} { "id" : 3,"subject" : "第三篇文章標題-替換后","content" : "第三篇文章內容-替換后" ,"author": "jam0"} { "update": { "_index": "test", "_type": "article", "_id": "4"} } { "doc" : {"content" : "第四篇文章內容-更新后"} }
delete 操作沒有請求體,所以后面緊跟另一個操作
三、Elasticsearch 的 DSL(特定領域語言) 查詢
以 json 請求體的形式查詢數據
1、基本查詢
查詢所有文檔
等同于 curl -XGET ‘http://localhost:9200/test/article/_search?pretty’
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "match_all": {} } }'#返回 { # 用時 毫秒 "took" : 4, "timed_out" : false, #分片信息 "_shards" : { "total" : 5, "successful": 5, "failed" : 0 }, "hits" : { #文檔數 "total" : 3, "max_score" : 1.0, "hits" : [ { "_index" : "test", "_type" : "article", "_id" : "AVf_6fM1vEkwGPLuUJqp", "_score" : 1.0, "_source" : { "id" : 2, "subject" : "第二篇文章標題", "content" : "第二篇文章內容", "author" : "jam" } }, { "_index" : "test", "_type" : "article", "_id" : "4", "_score" : 1.0, "_source" : { "id" : 4, "subject" : "第四篇文章標題", "content" : "第四篇文章內容-更新后", "author" : "tomi" } }, { "_index" : "test", "_type" : "article", "_id" : "3", "_score" : 1.0, "_source" : { "id" : 3, "subject" : "第三篇文章標題", "content" : "第三篇文章內容", "author" : "jam" } } ] } }
查詢作者是名字包含 “jam” 的文檔,返回 id 是 2和3 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "match": { "author": "jam" } } }'
查詢文章內容包含 “更新” 的文檔,返回 id 是 4 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "match": { "content": "更新" } } }'
2、過濾語句
1> term 過濾,主要用于精確匹配哪些值,比如數字,日期,布爾值或 not_analyzed 的字符串(未經分析的文本數據類型)
查詢作者是 jam 的文檔,返回 id 是 2 和 3 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "term": { "author": "jam" } } }'
2> terms 過濾,跟 term 有點類似,但 terms 允許指定多個匹配條件
查詢作者是 jam 、tomi 的文檔,返回 id 是2,3,4 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "terms": { "author": ["jam","tomi"] } } }'
3> range 過濾,指定范圍查找
查找 id 范圍是 大于等于2,小余4 的文檔,返回 id 是2,3 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "range": { "id": { "gte": 2, "lt": 4 } } } }'
gt:大于
gte :大于等于
lt:小于
lte:小于等于
4> bool 過濾
可以合并多個過濾條件查詢結果的布爾邏輯,它有三個操作符
操作符 | 釋義 |
---|---|
must | 多個查詢條件的完全匹配,相當于 and |
must_not | 多個查詢條件的相反匹配,相當于 not |
should | 至少有一個查詢條件匹配, 相當于 or |
查找作者是 jam (查出 id 為 2,3 的文檔),但是 id 不能為 2 的文檔,返回 id 為 3的文檔(should 就自己測試吧)
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "bool": { "must": { "term": { "author": "jam" }}, "must_not": { "term": { "id": 2 }} } } }'
提示:做精確匹配搜索時,你最好用過濾語句,因為過濾語句可以緩存數據
3、查詢語句
1> multi_match 查詢,同時搜索多個字段
查詢 subject 或者 content 字段中有 “更新” 二字的文檔,返回 id 為 4 的文檔,由于用的分詞器是 standard ,它會把 “更新” 拆成 “更”、”新” 來查找
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "multi_match": { "query": "更新", "fields": ["subject", "content"] } } }'
2> bool 查詢
與 bool 過濾相似,用于合并多個查詢子句。
查詢 內容中帶有 “第” ,但是不帶有 “新” 的文檔,返回 id 為 2,3 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "bool": { "must": { "match": { "content": "第" }}, "must_not": { "match": { "content": "新" }} } } }'
3、復合查詢
介紹完過濾語句和查詢語句,現在我們就來將它們兩個組合起來使用
請求體格式如下
{ "query" : { "filtered" : { "query" : { "match_all" : {} }, "filter" : { "term" : { "author" : "jam"} } } } }
filtered 中可以只包含 query 或 filter 或者兩者都存在,可存在多個 filter ,若不指定查詢范圍(query),默認為 “match_all” : {}
query 中可包含 bool 查詢或match 查詢
filter 中可包含各種過濾查詢
現在來看一個比較復雜的例子
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "filtered": { "query" : { "match_all" : {} }, "filter" : { "range": { "id": { "gte": 2, "lte": 4 } } }, "filter" : { "bool": { "must": { "term": { "author": "jam" }}, "must_not": { "term": { "id": 2 }} } } } } }'
首先是查詢所有數據(可省略),可改成有條件的查詢
"query" : { "match_all" : {} },
然后進行過濾查詢,查詢 id 范圍 大于等于 2 小于等于 4 的文檔
"filter" : { "range": { "id": { "gte": 2, "lte": 4 } } },
最后再過濾,查詢作者是 jam ,但是 id 不能是 2 的文檔,這樣就只有 id 為 3 的文檔符合條件
"filter" : { "bool": { "must": { "term": { "author": "jam" }}, "must_not": { "term": { "id": 2 }} } }
這條復合查詢類似如下 SQL 語句
SELECT * FROM article WHERE id >= 2 AND id <= 4 AND author = ‘jam’ AND id != 2
4、 驗證查詢語句
查詢語句的請求體越來越大,很容易在書寫過程中出現錯誤,可以使用 validate API 的explain 進行驗證
使用上面的復合查詢語句(注意請求鏈接)
curl -XGET 'http://localhost:9200/test/article/_validate/query?explain&pretty' -d ' { "query": { "filtered": { "query" : { "match_all" : {} }, "filter" : { "range": { "id": { "gte": 2, "lte": 4 } } },"filter" : { "bool": { "must": { "term": { "author": "jam" }}, "must_not": { "term": { "id": 2 }} } } } } }' #返回,真是正確的 { "valid" : true, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0}, "explanations" : [ { "index" : "test", "valid" : true, "explanation" : "+(+*:* #(+author:jam -id:`\b\u0000\u0000\u0000\u0002)) #ConstantScore(+ConstantScore(_type:article))" } ] } # 將第一個filter 上面的逗號去掉,再驗證。錯誤信息中會提示第幾行,第幾列出錯,方便修改錯誤 { "valid" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "explanations" : [ { "index" : "test", "valid" : false,"error" : "[test] QueryParsingException[Failed to parse]; nested: JsonParseException[Unexpected character ('\"' (code 34)): was expecting comma to separate OBJECT entries\n at [Source: org.elasticsearch.transport.netty.ChannelBufferStreamInput@3175024d; line: 8, column: 14]];; com.fasterxml.jackson.core.JsonParseException: Unexpected character ('\"' (code 34)): was expecting comma to separate OBJECT entries\n at [Source: org.elasticsearch.transport.net[email protected]; line: 8, column: 14]" } ] }
分析match 查詢(explanation)
curl -XGET 'http://localhost:9200/test/article/_validate/query?explain&explanation&pretty' -d ' { "query": { "match": { "content": "更新" } } }' #返回 { "valid" : true, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "explanations" : [ { "index" : "test", "valid" : true, "explanation" : "+(content:更 content:新) #ConstantScore(+ConstantScore(_type:article))" } ] }
explanations 中描述了 ES 將 “更新” 拆成 “更”、”新” 來查找
5、排序
默認情況下,結果集以 _score(相關性評分) 進行倒序排列(值越高越靠前)
可以使用 sort 指定排序字段
查詢 內容中包含 “第二更新” 的文檔,并按照 id 倒序排列
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "match": { "content": "第二更新" } }, "sort": { "id": { "order": "desc" } } }' #返回 { "took" : 96, "timed_out" : false,"_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : null, "hits" : [ { "_index" : "test", "_type" : "article", "_id" : "4", "_score" : null, "_source" : {"id" : 4, "subject" : "第四篇文章標題", "content" : "第四篇文章內容-更新后", "author" : "tomi" }, "sort": [ 4 ] }, { "_index" : "test", "_type" : "article", "_id" : "3", "_score" : null, "_source" : { "id": 3, "subject" : "第三篇文章標題", "content" : "第三篇文章內容", "author" : "jam" }, "sort" : [ 3 ] }, { "_index" : "test", "_type" : "article", "_id" : "AVf_6fM1vEkwGPLuUJqp", "_score" : null, "_source": { "id" : 2, "subject" : "第二篇文章標題", "content" : "第二篇文章內容", "author" : "jam" }, "sort" : [ 2 ] } ] } }
每個結果中多了個 sort 字段,就是以此來排序的,而 _score 的值為 null ,是因為計算 _score 是比較消耗性能的,而它通常主要用作排序,既然已經指定排序字段了,就不會計算 _score ,若要強制計算 _score ,可加入如下內容(與 query ,sort 同級)
"track_scores" : true
多級排序
按照 _score 倒序排序,若分數相同,再按照 id 倒序排序;因為使用了 _score 排序,所以會計算出 _score ,而不需要使用 “track_scores” : true
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "query": { "match": { "content": "第二更新" } }, "sort": [ { "_score": { "order": "desc" }}, { "id": { "order": "desc" }} ] }' #返回 ... "sort" : [ 0.05846126, 4 ] ... "sort" : [ 0.023869118, 2 ] ... "sort" : [ 0.0050183414, 3 ] ...
這里簡單介紹一下 _score 相關性評分值是如何得來的,這就涉及到了 ElasticSearch 的相似度算法( TF/IDF),即檢索詞頻率/反向文檔頻率,它包括
檢索詞頻率:檢索詞在該字段出現的頻率,出現頻率越高,相關性也越高
反向文檔頻率:一個詞在所有文檔中出現的頻率,出現的越頻繁,分數越低,因為像 “的” “我” 這類字出現得很頻繁,對相關性貢獻很低,相反,如 “ElasticSearch” 這類少見的詞對相關性貢獻很大,分數也就會越高
字段長度:字段越短,其權重就越高。檢索詞出現在一個較短的字段中比出現在一個較長的字段中所占的權重越高
參考文檔
我們可以使用 explain 來查看相關性評分 _score 的計算過程,將上面的請求鏈接改成 http://localhost:9200/test/article/_search?explain&pretty 請求體不變
這里截取一段返回值做解釋
# 截取 id 為 2 的一段分析 { "value" : 0.023869118, # 分析"第"字 "description" : "weight(content:第 in 0) [PerFieldSimilarity], result of:", "details" : [ { "value" : 0.023869118, "description" : "score(doc=0,freq=1.0), product of:", "details" : [ { "value" : 0.20743163, "description" : "queryWeight, product of:", "details" : [ { "value" : 0.30685282, "description" : "idf(docFreq=1, maxDocs=1)", "details" : [ ] }, { "value" : 0.67599714, "description" : "queryNorm", "details" : [ ] } ] }, { "value" : 0.11506981, "description" : "fieldWeight in 0, product of:", "details" : [ { "value" : 1.0, # 檢索詞頻率 檢查 "description" : "tf(freq=1.0), with freq of:", "details" : [ { "value" : 1.0, "description" : "termFreq=1.0", "details" : [ ] } ] }, { "value" : 0.30685282, # 反向文檔頻率 檢查 "description" : "idf(docFreq=1, maxDocs=1)", "details" : [ ] }, { "value" : 0.375, # 字段長度 檢查 "description" : "fieldNorm(doc=0)", "details" : [ ] } ] } ] } ] }
6、分頁與返回指定字段
分頁
size 返回的結果數
from 開始數(偏移量)
先按照 id 字段順序排序,然后從第二條(from)開始取,取兩條(size)數據,返回 id 為 4 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "sort" : { "id" : { "order" : "asc"}}, "from" : 2, "size" : 2 }'
返回指定字段的數據。有時候數據字段太多,而我們使用的只是其中的幾個字段,我么可以使用 _source 來指定返回的字段
查詢所有數據, 只返回作者和標題
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d ' { "_source" : ["author" ,"subject"] }'
四、參考資料
智能推薦
集合中出現的問題
1.Java集合框架的長處? 2.集合框架中的泛型有什么長處? 3.Java集合框架的基礎接口有哪些? 4.為何Map接口不繼承Collection接口? 5.Iterater和ListIterator之間有什么差別? 6.fail-fast與fail-safe有什么差別? 7.在迭代一個集合的時候,怎樣避免ConcurrentModificationException?8.在Java中,Hash...
三元運算符引起的bug
三元運算符引起的bug 前言 延伸 總結 前言 今天測試提交了一個bug,跟蹤發現了一段代碼,初看還沒什么問題,簡易后的代碼: 這段代碼想要實現的功能是返回 細看發現后面使用的是三元運算符,這就是造成這個bug的元兇。 解決方案:使用括號括起來。 延伸 在做這個例子的時候,使用ideal,習慣性的使用sonar掃描: 這里很明顯的提醒這里表達式有問題。 總結 三元運算符格式:expression1...
利用dom4j來生成xml
xml比較常用,處理xml的方式也比較多。現在就聊聊如何用dom4j來生成xml吧 先看看效果。。。 上傳代碼: 大概說下主要步驟吧: 1.創建document對象,創建root根節點,然后通過根節點繼續生成節點。其中也可以給該節點添加屬性,復制等。 2.創建輸出流,將該對象輸出到xml文件中去。需要設置好路徑和文件名等。...
Android輪播圖原理思路分析+實現方案
來自:http://blog.csdn.net/wubihang/article/details/52512597 ListView的headerView設置為輪播圖之后結合上/下拉刷新/加載的模式成為現在大多數APP的一個必須具備的功能,對于許多初學者來說想要實現輪播圖這樣一個集線程睡眠、自動處理、替換過程中刷新UI界面的組合功能非常困難,沒有思路,感覺無從下手,去搜索各種實現方案,發現目前充斥...
Lambda的編寫和使用---java8編程實戰
目錄 1.使用匿名類來表示不同的行為來實現行為參數,代碼有點啰嗦,解決這個問題的新工具--Lambda表達式。 2.Lambda的概念 3.Lambda語法測試 4.在程序中如何使用Lambda表達式 1.使用匿名類來表示不同的行為來實現行為參數,代碼有點啰嗦,解決這個問題的新工具--Lambda表達式。 Lambda可以讓你很簡潔地表示一個行...
猜你喜歡
Electron學習筆記[1]
什么是Electron Electron(最初名為Atom Shell[3])是GitHub開發的一個開源框架。它允許使用Node.js(作為后端)和Chromium(作為前端)完成桌面GUI應用程序的開發。 Electron 可以讓你使用純 JavaScript 調用豐富的原生 APIs 來創造桌面應用。你可以把它看作一個專注于桌面應用的 Node.js 的變體,而不是 Web 服務器。 很多很...
SpingCloud踩坑記(二)SpringCloud配置中心
springCloud配置中心 官網介紹如下 簡單來說:springCloud config項目,用來為分布式的微服務系統中提供集成式外部配置支持,分為客戶端和服務端。并且通過配置服務中心集中配置不同環境的變量,方便管理和配置遷移。 大致流程如下 涉及到三個角色: 配置中心服務端:為配置客戶端提供對應的配置信息,配置信息的來源是配置倉庫。應用啟動時,會從配置倉庫拉取配置信息緩存到本地倉庫中。 配置...
按模板導出Excel
在項目需求中,經常會遇到導出Excel,一般沒有模板的Excel很容易導出,那如果遇到格式很復雜的Excel怎么處理呢 例如下面這種: 我們使用 aspose很容易實現 需要jar包aspose-cells-8.5.2.jar 工具類 controller 編輯模板 導出效果如下...