Elasticsearch开篇-环境搭建

说明

  1. 建议将整篇文章看完后再进行操作,文章结尾有我遇到的坑及解决方案,可以看看!
  2. 务必在java安装好的环境下进行

一、准备工作:

  1. Ubuntu 16.04.2 x86_64
  2. Elasticsearch 5.1.2 下载地址:https://artifacts.elastic.co/...
  3. Kibana 5.1.2 下载地址:https://artifacts.elastic.co/...
  4. 由于oracal下载java太慢,可以到CSDN下载 https://download.csdn.net/dow...
  5. 在root权限下操作创建用户:
    创建elasticsearch组:groupadd elasticsearch
    创建elasticsearch用户:useradd elasticsearch -g elasticsearch
    创建elasticsearch家目录:mkdir /home/elasticsearch
    改变家目录权限:chown -R mkdir elasticsearch:elasticsearch /home/elasticsearch

二、开始搭建

1. Elasticsearch 5.1.2:

-  下载Es:`wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.2.tar.gz`
- 解压Es到指定目录:`tar -zxf elasticsearch-5.1.2.tar.gz -C /usr/local/`
- 改变目录权限:`chown -R elasticsearch:elasticsearch  elasticsearch-5.1.2/`
- 在Java环境变量配置好的情况下执行后台启动es:`sudo ./bin/elasticsearch -d -p pid`
- 关闭es:``kill `cat pid```   #pid是后台启动es时的进程号,文件是在/usr/local/elasticsearch-5.1.2/pid
- 在服务器执行:`curl localhost:9200`
- 如果有如下json返回表示安装成功:
```json
{
  "name" : "9bUWQKn",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "2J3aD8mISy2A6l7JP6T4cA",
  "version" : {
    "number" : "5.1.2",
    "build_hash" : "c8c4c16",
    "build_date" : "2017-01-11T20:18:39.146Z",
    "build_snapshot" : false,
    "lucene_version" : "6.3.0"
  },
  "tagline" : "You Know, for Search"
}
```

2、Kibana 5.1.2

- 下载Kibana:`wget https://artifacts.elastic.co/downloads/kibana/kibana-5.1.2-linux-x86_64.tar.gz`
- 解压:`tar -zxf kibana-5.1.2-linux-x86_64.tar.gz -C /usr/local/`
- `cd /usr/local/kibana-5.1.2-linux-x86_64`
- 改变kibana的拥有者为elasticsearch:`sudo chown elasticsearch:elasticsearch -R kibana-5.1.2-linux-x86_64/`
- 启动kibana:`./bin/kibana`
- 访问:用浏览器http://192.168.10.10:5601/ 此时会进不去页面
    - 原因:kibana的默认限制了访问ip
    - 解决:`vim /usr/local/kibana-5.1.2-linux-x86_64/config`
      将:server.host前的注释打开,最终是:server.host: "0.0.0.0"要重启kibana:`./bin/kibana`
    - 再次访问:http://192.168.10.10:5601/   就ok了额外说下:有了kibana了就不用安装sense这个插件了,因为kibana 5.1.2默认集成了sense

三、用chrome装es的head扩展

不用elasticsearch-plugin安装head了,直接Chrome扩展:https://chrome.google.com/web...

四、坑

1、用root用户权限运行es会报:

在安装es的目录/usr/local/elasticsearch-5.1.2运行es:./bin/elasticsearch

会报如下错误:
root@homestead:/usr/local/elasticsearch-5.1.2# ./bin/elasticsearch
2018-03-26T15:17:52,954[o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
原因:es不能以root用户启动
解决:切换成elasticsearch用户 su elasticsearch

2、运行./bin/elasticsearch -d -p pid会报

elasticsearch@homestead:/usr/local/elasticsearch-5.1.2$ Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.

原因:我是在Mac下安装了Virtual Box,只分配了2g的内存,这是由于内存不足引起的
解决:
修改es的配置文件:vim /usr/local/elasticsearch-5.1.2/config/jvm.options
将:-Xms2g 改为 -Xms1g,如果1g还不够的话,可以改为-Xms512m
将:-Xmx2g 改为 -Xmx1g,如果1g还不够的话,可以改为-Xmx512m
修改完jvm.options后,再执行./bin/elasticsearch -d -p pid

3、./bin/elasticsearch -d -p pid会报

elasticsearch@homestead:/usr/local/elasticsearch-5.1.2$ 2018-03-26 15:44:22,462 main ERROR RollingFileManager (/usr/local/elasticsearch-5.1.2/logs/elasticsearch.log) java.io.FileNotFoundException: /usr/local/elasticsearch-5.1.2/logs/elasticsearch.log (Permission denied) java.io.FileNotFoundException: /usr/local/elasticsearch-5.1.2/logs/elasticsearch.log (Permission denied)

原因:权限不够,导致/usr/local/elasticsearch-5.1.2/logs/elasticsearch.log日志文件以elasticsearch的文件没权限写入
解决:sudo chown -R elasticsearch:elasticsearch elasticsearch-5.1.2
再次执行:./bin/elasticsearch -d -p pid

4、在启动es时报:

main ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.RollingFileAppender

原因:es是在java环境下运行的,所以要先安装java,并配置好相应的环境变量
解决:以下操作在root用户下操作将java解压到要安装的目录:/usr/local/jdk1.8.0_161
配置java环境变量
vim /etc/profile
在/etc/profile文件末尾添加如下几行,根据你所安装的目录修改
JAVA_HOME=/usr/local/jdk1.8.0_161
CLASSPATH=.:$JAVA_HOME//usr/local/jdk1.8.0_161/lib/tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
再执行source /etc/profile生效

5、kibana的版本和es的版本务必一致

如:Elasticsearch 5.1.2与Kibana 5.1.2,版本都5.1.2,不同版本的话,在启动kibana时会报错

6、启动es时报

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

临时解决:sudo sysctl -w vm.max_map_count=262144 #重启linux会失效
永久解决:sudo vim /etc/sysctl.conf
加入:vm.max_map_count=262144
再重启linux生效

7、用chrome访问192.168.10.10:9200时出进入不了页面

原因:es的配置文件限制了指定的ip访问,默认是服务器的locallhost:9200
解决:
sudo vim /usr/local/elasticsearch-5.1.2/config/elasticsearch.yml
将:network.host的注释打开,最终是network.host: 0.0.0.0,表示不限制访问ip
kill `cat pid` #杀掉es进程
./bin/elasticsearch -d -p pid #启动es
再用浏览器访问:192.168.10.10:9200

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装Elasticsearch和Python Elasticsearch客户端 - Elasticsearch安装可以参考官网的[安装文档](https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html) - Python Elasticsearch客户端可以使用pip安装:`pip install elasticsearch` 2. 在Django项目中创建一个app,用于处理与Elasticsearch相关的逻辑。 3. 配置Elasticsearch连接信息,可以在Django的`settings.py`中添加以下配置: ``` ELASTICSEARCH_DSL = { 'default': { 'hosts': 'localhost:9200' }, } ``` 其中,`hosts`就是Elasticsearch的地址,这里使用默认的`localhost:9200`。 4. 创建Elasticsearch索引模板,可以在app目录下创建一个`search_indexes.py`文件,定义索引模板: ``` from elasticsearch_dsl import Document, Text, Date, Keyword class ArticleIndex(Document): title = Text() content = Text() pub_date = Date() tags = Keyword(multi=True) class Index: name = 'articles' ``` 其中,`ArticleIndex`是一个继承自`Document`的类,定义了索引的字段和类型。`Index`类中的`name`属性指定了索引的名称。 5. 在app目录下创建`serializers.py`文件,定义序列化器,将模型序列化为Elasticsearch索引模板: ``` from rest_framework import serializers from .models import Article from .search_indexes import ArticleIndex class ArticleIndexSerializer(serializers.ModelSerializer): class Meta: model = Article fields = ('id', 'title', 'content', 'pub_date', 'tags') def save(self, **kwargs): article = super().save(**kwargs) article_index = ArticleIndex(meta={'id': article.id}, **article.__dict__) article_index.save() return article ``` 其中,`ArticleIndexSerializer`继承自`ModelSerializer`,定义了序列化的模型和字段。在`save`方法中,先保存模型,再将模型数据序列化为Elasticsearch索引模板,最后保存到Elasticsearch中。 6. 在app目录下创建`views.py`文件,定义视图函数,实现Elasticsearch搜索功能: ``` from rest_framework.views import APIView from rest_framework.response import Response from elasticsearch_dsl import Q from .search_indexes import ArticleIndex from .serializers import ArticleIndexSerializer class ArticleSearchView(APIView): def get(self, request): query = request.query_params.get('q', '') s = ArticleIndex.search().query( Q('multi_match', query=query, fields=['title', 'content', 'tags']) ) response = [] for hit in s.execute().hits: serializer = ArticleIndexSerializer(data=hit.to_dict()) serializer.is_valid() response.append(serializer.data) return Response(response) ``` 其中,`ArticleSearchView`继承自`APIView`,定义了一个`get`方法,接收`q`参数作为搜索关键词。通过Elasticsearch DSL构建查询语句,搜索索引模板中的数据,最后将搜索结果序列化返回。 7. 在app目录下创建`urls.py`文件,定义路由: ``` from django.urls import path from .views import ArticleSearchView urlpatterns = [ path('search/', ArticleSearchView.as_view(), name='article_search'), ] ``` 其中,`search`路由对应了`ArticleSearchView`视图函数。 8. 在Django的`settings.py`中添加app,配置数据库信息: ``` INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'app_name', # 添加app ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'db_name', 'USER': 'db_user', 'PASSWORD': 'db_password', 'HOST': 'localhost', 'PORT': '3306', } } ``` 9. 在app目录下创建`models.py`文件,定义模型,使用Django ORM作为数据源: ``` from django.db import models class Article(models.Model): title = models.CharField(max_length=255) content = models.TextField() pub_date = models.DateTimeField(auto_now_add=True) tags = models.CharField(max_length=255, blank=True) def __str__(self): return self.title ``` 其中,`Article`是一个继承自`models.Model`的类,定义了文章的属性。 10. 在app目录下创建`admin.py`文件,注册模型到Django Admin中: ``` from django.contrib import admin from .models import Article admin.site.register(Article) ``` 11. 在Django的`urls.py`中添加app的路由: ``` from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('api/', include('app_name.urls')), ] ``` 12. 启动Django开发服务器,访问`http://localhost:8000/admin/`,进入Django Admin,创建一些文章数据。 13. 访问`http://localhost:8000/api/search/?q=django`,可以看到搜索结果,其中包含关键词`django`的文章数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值