In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
I believe many inexperienced people don't know what to do about how to install Elasticsearch and how to use it in Python. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The first sentence of the introduction to Elasticsearch on the official website:
Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases.
Elasticsearch is a distributed RESTful-style search and data analysis engine that can solve the emerging use cases. The underlying layer of Elasticsearch is the open source library Lucene. Lucene is a lower-level search engine, but you can't use Lucene directly. You have to write your own code to call its interface. Elasticsearch is the encapsulation of Lucene, which provides the operation interface of REST API and can be used out of the box. It can be understood that Lucene is like Baidu, and then you develop a more advanced and convenient search engine on the basis of Baidu, which is Elasticsearch. Elasticsearch can be regarded as a NoSQL database, which provides basic functions such as adding, deleting, changing and querying data. What is NoSQL?
"Next Generation Databases mostly addressing some of the points: being non-relational, distributed, open-source and horizontally scalable
The next generation of databases mainly solve the following problems: non-relational, distributed, open source and flat extensible.
OK, this is the end of the basic concept. If you do not understand and do not affect your use of ElasticSearch, you can simply understand that it is a database that can store data, but the stored data is not structured data such as MySQL. The most important thing is that the retrieval speed of ElasticSearch is very fast, basically checking a piece of data in milliseconds. There is also a Lucene-based search engine called Solr, which is the Java search engine server.
Today, I will mainly introduce how to install and use it in Python.
1. Install-Ubuntu
1.1 ElasticSearch
Download address: https://www.elastic.co/cn/downloads/elasticsearch
Just download the zip file directly. After downloading, cd to the folder where you installed it, and then execute:
. / bin/elasticsearch
This opens the ElasticSearch service
Then execute:
Curl http://localhost:9200/
If curl is not installed here, it needs to be installed first.
1.2 Kibana
Kibana is an ElasticSearch management tool that can visualize the data in Elasticsearch and download it by the way. This is very easy to use, a lot of functions, I am also groping.
Address: https://www.elastic.co/cn/products/kibana
It will be the same after downloading. Cd to the extracted file and execute
. / bin/kibana
Then open a browser and type:
Http://localhost:5601
Then you can see a more handsome interface, there is a Dev option on the left, here you can write a variety of add, delete, modify and query statements.
2. Use Elasticsearch in Python
2.1 installation
The Elasticsearch module in Python is the Python-based client of Elasticsearch. Installation is very simple, just use pip, now the installed version is 6.0.0. (2018-1-5)
Pip install elasticsearch or pip3 install elasticsearch
2.2 create an index
From elasticsearch import Elasticsearch
Es = Elasticsearch (hosts= "localhost:9200")
# the default here is port 9200, if you want to save the data to
# somewhere else (server), just change this address.
Es.create (index='test_index'
Doc_type='post')
2.3 storing data in ES
There are three ways to store data, one is to store the data directly with index, and the other is to use the create method under the indices class to store the data directly if the index exists, create the index if it does not exist, and then save it. Another method is to use the bulk method of batch storage, which is a more common method. You can choose according to your actual task.
From elasticsearch import Elasticsearch
From elasticsearch.helpers import bulk
Es = Elasticsearch (hosts= "localhost:9200")
Index_name = "test_index"
Try:
# you can make an index before saving the data each time
# whether there is a judgment to prevent conflict.
Es.indices.delete (index=index_name)
Except:
Pass
# single entry
Es.index (index= "test_index"
Doc_type= "post"
Id=1
Body= {
Here is your data: here is your data.
)
# or
Es.indices.create (index=index_name
Body= {
Here is your data: here is your data.
)
# batch storage
With open ("file_test.txt",'r') as fr:
Lines = fr.readlines ()
I = 0
Bulk_data = []
For line in lines:
Data = {
"_ index": "test_index"
"_ type": "post"
"_ source": {
"content": line}
}
Bulk_data.append (data)
I + = 1
# every 10000 pieces of data are saved.
If I% 10000 = 0:
Bulk (es, bulk_data)
Bulk_data = []
2.3 retrieve data
When the data is stored in ES, it is retrieved, otherwise it is really useless if it is not used, is it? Retrieval is also relatively simple, mainly to sort out the logical relationship is, really do not understand, try more. Give me a chestnut.
Body = {
"query": {
"match": {
"title": {
"query": "China"
}
}
}
}
Es.search (index= "test_index"
Doc_type= "post"
Body=body)
ElasticSearch is mainly used for retrieval, you can understand it as a Baidu. Therefore, it is important to be proficient in the use of retrieval data. When searching, it is mainly what is written in that body, in which query,match is the keyword, this cannot be modified, title is the field name defined when you store it, and then "China" is the content you want to retrieve.
After reading the above, have you mastered how to install Elasticsearch and how to use it 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.