Elasticsearch 环境搭建
1. 安装
1.1 环境:Ubuntu 16.04
1.2 官方文档:www.elastic.co
1.3 安装Java
确保 java 的版本在 java8 以上
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
1.4 安装 elaseticsearch
sudo apt-get install elasticsearch
1.5 启动 elasticsearch 服务
sudo -i service elasticsearch start
sudo -i service elasticsearch stop
下面的命令检查运行成功
curl -XGET 'localhost:9200/?pretty'
2. 配置
2.1 内存占用
为了发挥ES的检索速度,建议ES单独配置在一台机器上,并将内存开到最大。若机器上还有其它程序运行,建议内存开到50%以下。修改最大内存占用方法:
vim /etc/elasticsearch/jvm.options
将内容中的
-Xms2g # Set the minimum heap size to 2g / 1500m.
-Xmx2g # Set the maximum heap size to 2g / 1500m.
修改后重启服务,可以通过 ps -ef|grep elastic
来查看修改结果。
2.2 网络配置
允许访问的网络配置:
vim /etc/elasticsearch/elasticsearch.yml
只本机访问:
network.host: 127.0.0.1
http.port: 9200
内网访问:
network.host: 内网ip地址
http.port: 9200
外网访问:
network.host: 外网地址 or 0.0.0.0
http.port: 9200
3. 使用
# 查看总文档数
curl -XPOST 'localhost:9200/chatbot/fromse/_count?pretty'
# 查询某一条问题
curl -XPOST 'esServer:9200/chatbot/fromse/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"question.keyword": {
"query": "周杰伦是干什么的",
"slop": 0
}
}
},
{
"term": {
"domain": "normal"
}
}
]
}
}
}'
# 查看 mapping
curl -XGET 'esServer:9200/chatbot/fromse/_mapping?pretty'
# 插入一条文档
curl -XPUT 'esServer:9200/chatbot/fromse/1' -d '
{
"id": 1,
"tCreated": "2017-03-09",
"tUpdate": "2017-03-09",
"tQuote": "2017-03-09",
"quote": 2,
"domain": "normal",
"site": "sogou",
"question": "",
"content": "我叫狗不理"
}'