python elasticsearch7.9

#书上例子,但是老版本es.修改后现在用es7.9最新环境运行
from elasticsearch import Elasticsearch
import time

index_settings = {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index": {
        "analysis": {
            "analyzer": {
                "keyword_analyzed": {
                    "type": "custom",
                    "filter": [
                        "lowercase",
                        "asciifolding"
                    ],
                    "tokenizer": "keyword"
                }
            }
        }
    }
}
doc_mapping = {
    "properties": {
        "skills": {
            "type": "text"
        }
    }
}

es = Elasticsearch('ip:9200')
index_name = 'books'
doc_type = "search"
body = {}
body['settings'] = index_settings
body['mappings'] = doc_mapping

if not es.indices.exists(index = index_name):
    print("create index")
    es.indices.create(index=index_name, body=body)
    time.sleep(2)
else:
    print('index exists')

doc1 = {
    'name' : 'Elasticsearch Essentials',
    'category' : ['Big Data', 'search engines', 'Analytics'],
    'Publication' : 'Packt-Pub',
    'Publishing Date' : '2015-31-12'
}
es.index(index=index_name, body=doc1, id='123')
response = es.get(index=index_name, id='123', ignore=404)
print(type(response), response['_source'])

script ={"script" : "ctx._source.category.add(\"pytest1\")"}
es.update(index=index_name, body=script, id='123')

response = es.get(index=index_name, id='123', ignore=404)
print(type(response), response['_source'])

猜你喜欢

转载自blog.51cto.com/hjun169/2533711
今日推荐