Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to operate elasticsearch in python

Shulou Source: shulou.com Published: 2022-06-02 08:21:15 10月13日 Update

It is believed that many inexperienced people have no idea about how to operate elasticsearch in python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The third-party library used to manipulate elasticsearch with python is elasticsearch.

1.python connects to elasticsearch for search

From elasticsearch import Elasticsearches = Elasticsearch ([{'host': settings.ELASTICSEARCH_HOST,' port': settings.ELASTICSEARCH_PORT}], timeout=3600) # connection esbody =\ {"query": {"bool": {"filter": {"terms": {"type_id": [1,3,4,5,6]}} "must_not": {"terms": {"_ id": must_not_list}} "must": {"range": {"video_date": {"gte": "now-24h+8h" "lte": "now+8h"}, "sort": {"video_like": {"order": "desc"}, "from": start, "size": size} data = es.search (body=body Index=settings.ELASTICSEARCH_INDEX)

Host is the host of ES IP,port is the port number of ES, and search is to call the search method. Body is the body of the search, index is the name of the index to be searched, and the structure of the returned result is in json format, the same as the result returned by directly manipulating es.

2. Python connection elasticsearch add documents

Es = Elasticsearch ([{'host': settings.ELASTICSEARCH_HOST,' port': settings.ELASTICSEARCH_PORT}], timeout=3600) action = {"type_id": now_ameme_data.get ("type_id"), "video_comment": now_ameme_data.get ("video_comment"), "share_count": now_ameme_data.get ("share_count") Video_url: now_ameme_data.get ("video_url"), "category_id": now_ameme_data.get ("category_id"), "video_title": now_ameme_data.get ("video_title"), "user_id": now_ameme_data.get ("user_id"), "price": now_ameme_data.get ("price") Video_like: now_ameme_data.get ("video_like"), "video_date": now_ameme_data.get ("video_date"), "updata_time": now_ameme_data.get ("update_time"), "video_img_url": now_ameme_data.get ("video_img_url"), "label": label} es.index (index= "aweme_smart", body=action, id=aweme_id)

The new document uses the index method, or you can specify an id for the document. The fields in action should correspond to the fields in the es document.

After reading the above, have you mastered the method of how to operate elasticsearch in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Documents methods searches content fields more results problems helpless for this host reason slogan name this skills format third party article index Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Linux Shulou Tech Info macOS