Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate elasticsearch in python

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report