elasticsearchpython
㈠ 講一下elasticsearch是怎麼使用的
今天細說一下elasticsearch的update更新功能,以及如何利用script腳本更新數據。
想要使用script腳本功能,需要在配置文件elasticsearch.yml里設置
python
script.disable_dynamic: false
關於elasticsearch script的文章,總是會沒完沒了的修改
ES支持更新,但是更新的方式是通過一個提供的腳本進行的。ES的做法是,通過
index找到相應的存放記錄的節點,然後執行腳本,執行完之後,返回新的索引。實際上執行的是一個get和reindex的過程,在這個過程中,通過
versioning來控制沒有其它的更新操作(這個功能是0.19後可用的)。具體實現的原理應該和elasticsearch
Versioning相關。
get,reindex的含義是,ES先取出這條記錄,然後根據新數據生成新記錄,然後在把新記錄放回到ES中(並不會覆蓋老的記錄)。
現在沒有數據,首先我們需要創建一條記錄
Python
$ curl -XPUT localhost:9200/xiaorui.cc/blog/1 -d '{
"counter" : 1,
"tags" : ["red"]
}'
$ curl -XPUT localhost:9200/xiaorui.cc/blog/1 -d '{
"counter" : 1,
"tags" : ["red"]
}'
直接修改數據,一定要注意,直接update的化,會覆蓋以前的數據,另外update的時候,需要/index/type/id ,一定要帶著id。 elasticsearch 應該不支持搜索query方式update修改數據。
Python
curl -XPUT 'localhost:9200/xiaorui.cc/blog/1?pretty' -d '
{
"name": "xiaorui.cc"
}'
curl -XPUT 'localhost:9200/xiaorui.cc/blog/1?pretty' -d '
{
"name": "xiaorui.cc"
}'
elasticsearch提供了doc這個局部更新參數,他可以局部修改,而不會直接覆蓋以前的數據,這會針對特定的k v,欄位修改。
Python
curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update?pretty' -d '
{
"doc": { "name": "ruifengyun" }
}'
curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update?pretty' -d '
{
"doc": { "name": "ruifengyun" }
}'
當Elasticsearch API不能滿足要求時,Elasticsearch允許你使用腳本實現自己的邏輯。腳本支持非常多的API,例如搜索、排序、聚合和文檔更新。腳本可以通過請求的一部分、檢索特殊的.scripts索引或者從磁碟載入方式執行。
下面是es script的用法,這些腳本是groovy開發的。 下面的語句的意思是說,將counter的值加4
Python
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{
"script" : "ctx._source.counter += count",
"params" : {
"count" : 4
}
}'
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{
"script" : "ctx._source.counter += count",
"params" : {
"count" : 4
}
}'
通過上面的例子,我們知道tags是個列表,如果用doc局部更新的語法,他是無法做到append的,還是會覆蓋tags這個欄位。 那麼怎麼實現列表擴展? 請使用elasticsearch script實現。
Python
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{
"script" : "ctx._source.tags += tag",
"params" : {
"tag" : "white"
}
}'
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{
"script" : "ctx._source.tags += tag",
"params" : {
"tag" : "white"
}
}'
_update也支持upsert功能,沒有這個欄位或者key,也會添加這個記錄。下面是一個例子,如果沒有counter欄位,則插入該欄位:
Python
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{
"script" : "ctx._source.counter += count",
"params" : {
"count" : 4
},
"upsert" : {
"counter" : 1
}
}'
$ curl -XPOST 'localhost:9200/xiaorui.cc/blog/1/_update' -d '{
"script" : "ctx._source.counter += count",
"params" : {
"count" : 4
},
"upsert" : {
"counter" : 1
}
}'
下面我們來復雜點的groovy script腳本用法. 當你的source沒有china這個key,那麼我會增加一個kv
Python
curl -XPOST "" -d'
{
"script": "if (!ctx._source.containsKey(\"china\")) { ctx._source.attending = newField }",
"params" : {"newField" : "blue" },
"myfield": "data"
}'
curl -XPOST "" -d'
{
"script": "if (!ctx._source.containsKey(\"china\")) { ctx._source.attending = newField }",
"params" : {"newField" : "blue" },
"myfield": "data"
}'
下面的script語法相對復雜的,會遍歷一組字典,然後進行判斷賦值。
{
「55555″: 22,
「name」: 「lisi」,
「distr_pan」: [
{
「k」: 15,
「v」: 15
},
{
「k」: 20,
「v」: 20
}
]
}
Python
$ curl -XPUT 'localhost:9200/xiaorui.cc/blog/9123/_update' -d '
{
"script" : "def x = false;ctx._source.distr_pan.each({if(it.get('k')==target){x=true}});if(x){ctx._source.distr_pan +=v}",
"params":{
"v":{"k":nlp, "v":35},
"target":15
}
}
$ curl -XPUT 'localhost:9200/xiaorui.cc/blog/9123/_update' -d '
{
"script" : "def x = false;ctx._source.distr_pan.each({if(it.get('k')==target){x=true}});if(x){ctx._source.distr_pan +=v}",
"params":{
"v":{"k":nlp, "v":35},
"target":15
}
}
elasticsearch script就講解到這里了,很多例子已經簡單明了…
script貌似不是很安全,最少遠程代碼執行的漏洞暴露過幾次了. 下次把python版的script走一遍試試.
貌似對於我們你者來說,不管是groovy python,沒什麼太大卻別,語法看起來都一個模子。
㈡ elasticsearch python 為什麼帶u
因為是Unicode字元啊,python中用u 表示unicode字元集,我們平時的環境里大多是utf-8或者gbk,但是ES里用的都是Unicode字元集,所以你的Python向ES寫東西都要用這個字元集,不然字元語言不通。
㈢ python 怎麼刪除elasticsearch的索引
curl -XDELETE 'http://host.IP.address:9200/logstash-*'1
如上是刪除所有以logstash-開始的索引。
㈣ python怎麼定義elasticsearch的類型
1
curl -X POST -d '{"title":"jones","amount":5.7}'
但是聽說,1.x之後不能直接curl,這不是重點忽略
下面介紹一個python使用elasticsearch的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from datetime import datetime
from elasticsearch import Elasticsearch
#連接elasticsearch,默認是9200
es = Elasticsearch()
#創建索引,索引的名字是my-index,如果已經存在了,就返回個400,
#這個索引可以現在創建,也可以在後面插入數據的時候再臨時創建
es.indices.create(index='my-index',ignore)
#{u'acknowledged':True}
㈤ python elasticsearch api scroll怎麼少10條記錄
ElasticSearch是一個基於Lucene的搜索伺服器,它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web介面。
下面介紹了利用Python API介面進行數據查詢,方便其他系統的調用。
must:所有分句都必須匹配,與 AND 相同。
must_not:所有分句都必須不匹配,與 NOT 相同。
should:至少有一個分句匹配,與 OR 相同。
㈥ Python主要內容學的是什麼
第一步:Python開發基礎
Python全棧開發與人工智慧之Python開發基礎知識學習內容包括:Python基礎語法、數據類型、字元編碼、文件操作、函數、裝飾器、迭代器、內置方法、常用模塊等。
Python全棧開發與人工智慧之Python高級編程和資料庫開發知識學習內容包括:面向對象開發、Socket網路編程、線程、進程、隊列、IO多路模型、Mysql資料庫開發等。
第三步:前端開發
Python全棧開發與人工智慧之前端開發知識學習內容包括:Html、CSS、JavaScript開發、Jquery&bootstrap開發、前端框架VUE開發等。
第十步:高並發語言GO開發
Python全棧開發與人工智慧之高並發語言GO開發學習內容包括:GO語言基礎、數據類型與文件IO操作、函數和面向對象、並發編程等。
㈦ python elasticsearch 怎麼用
標准語法
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])
res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])
es.indices.refresh(index="test-index")
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
可以參考官方文檔
https://elasticsearch-py.readthedocs.io/en/master/
㈧ 將elasticsearch加入到python中需要配置環境嗎
1.內存:物理機內存最好能在64G,ES進程的最大堆內存配置不要超過32G.另外需要關閉交換內存 swapoff -a.
2.CPU:選擇多核心cpu。即使單核cpu主頻低一些也要選擇多核心的cpu。
3.Disk:最好選擇SSD硬碟,磁碟陣列最好選擇RAID0,因為ES自身對數據做過冗餘。另外避免使用NFS掛載。
4.network:數據中心的帶寬保證在1 GbE,10 GbE,避免一個集群跨多個數據中心。