Вот несколько полезных команд для людей, начинающих наasticsearch или обновляющих до последней версии
import requests
import json
headers1 = {'Content-Type':'application/json'}
#create index
url = 'http://localhost:9200/my_index'
response = requests.put(url)
#view indexes
url = 'http://localhost:9200/_cat/indices?v'
response = requests.get(url)
#create mappings. if mappings are not created, then dynamic mapping will be created by elastic search
url = 'http://localhost:9200/my_index/_mapping'
response = requests.post(url, headers=headers1, data=json.dumps(data_obj))
data_obj = {
"properties": {
"names": {
"type": "search_as_you_type",
}
}
}
#insert data. if mappings have not been created initially, then dynamic mapping will be created by elastic search
url = 'http://localhost:9200/my_index/_doc/'
my_list = ['test name 1', 'test name 2', 'test name 3']
for i,j in enumerate(my_list):
response = requests.post(url+str(i), headers=headers1, data=json.dumps({"names":j}))
print(response.content)
#retrieve data.
url = 'http://localhost:9200/my_index/_doc/_search'
url = 'http://localhost:9200/my_index/_search' #works with both
data_obj = {
"query": {
"multi_match": {
"query": "test name 2",
"fields": [
"names",
"names._2gram",
"names._3gram"
]
}
}
}
response = requests.get(url, headers=headers1, data=json.dumps(data_obj))
print(response.content)
x = json.loads(response.content)
#x = json.loads(response.content.decode("utf-8")) #in case the response is in bytes and not str
for i in x["hits"]["hits"]:
print(i["_source"]["names"], i["_score"])
Я новичок в поиске elasti c. Будет продолжать обновлять пост с новыми основами