ElasticSearch常用操作記錄

2021-10-06 20:09:58 字數 3259 閱讀 6960

es使用記錄

# -*- coding:utf-8 -*-

import pandas as pd

from elasticsearch import elasticsearch

from elasticsearch.helpers import bulk

import argparse

class

elasticobj

:def

__init__

(self, index_name, index_type, ip=

"127.0.0.1"):

''' 構建es索引,批量匯入資料

'''self.index_name = index_name

self.index_type = index_type

self.es = elasticsearch(

[ip]

)def

bulk_index_data

(self, csvfile)

:'''

用bulk將批量資料儲存到es

'''df =

open

(csvfile,

'r', encoding=

'utf8'

) lines = df.readlines(

)print

(len

(lines)

) doc =

for item in lines:

it = item.split(

'\t'

) dic =

dic[

'docid'

]= it[0]

dic[

'passage'

]= it[3]

.strip(

).replace(

' ','')

actions =

i =0for line in doc:

action =

} i +=

1print

('index_num:'

, i)

success, _ = bulk(self.es, actions, index=self.index_name, raise_on_error=

true

, request_timeout=

1000

)print

('performed %d actions'

% success)

defcreate_index

(self, index_name, index_type)

:'''

建立索引

'''# 建立對映

,"docid":}

}}# 構建索引

print

("begin construct index"

)if self.es.indices.exists(index=self.index_name)

isnot

true

:print

(res)

else

:print

("exists"

)if __name__ ==

"__main__"

:#建立es,把文件批量匯入索引節點

obj = elasticobj(

"passage"

,"_doc"

)print

("init done!"

) obj.create_index(

"passage"

,"_doc"

)print

("create done!"

) obj.bulk_index_data(

"./datasets/law/new_doc.txt"

)

# -*- coding:utf-8 -*-

import pandas as pd

from elasticsearch import elasticsearch

from elasticsearch.helpers import bulk

class

elasticobj

:"""

es索引類,用於檢索資料

"""def__init__

(self, index_name,index_type,ip =

"127.0.0.1"):

self.index_name =index_name

self.index_type = index_type

self.es = elasticsearch(

[ip]

)"""

通過問題檢索文件

"""defget_data_by_body

(self, question, k)

: doc =}}

try:

_searched = self.es.search(index=self.index_name, doc_type=self.index_type, body=doc)

answers =

for item in _searched[

'hits'][

'hits']:

(item[

'_source'][

'passage'][

:20], item[

'_source'][

'docid'])

)return answers

except

:print

('search not exist'

)print

(question)

defsearch

(query,topk)

: obj = elasticobj(

"passage"

,"_doc"

) answers = obj.get_data_by_body(query, topk)

return answers

if __name__ ==

"__main__"

:

query =

''print

(search(query,3)

)

curl localhost:9200/_cat/indices?v

elasticsearch常用操作

es模板postman常用操作 類別方法 url描述 查詢模板get http ip 9200 template template 查詢模板中template開頭的所有模板 為萬用字元 查詢特定的模板get http ip 9200 template vias business查詢名為vias bu...

elastic search常用操作

常用操作 查詢所有索引及狀態 get cat indices?v 查詢productsearchuat索引,預設返回一條資料 get productsearchuat search 知道product id 查es索引 get productsearchuat search 通過企業名稱查詢企業 g...

elasticsearch常用curl鏈結

1 測試es是否啟動成功 pretty引數是為了讓查詢結果更方便閱讀 3 建立megacorp索引 employee型別 id為1的資料 索引名必須小寫,不能以下劃線開頭,不能包含逗號。型別命名可以是大寫或者小寫,但是不能以下劃線或者句號開頭,不應該包含逗號,並且長度限制為256個字元。如果該id的...