Elasticsearch 环境搭建

本文详细介绍如何在Ubuntu16.04环境下安装Elasticsearch并进行必要的配置,包括Java环境准备、服务启动与停止、内存及网络配置等。同时提供了基本的使用示例,如查询文档数量、搜索特定问题等。

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": "我叫狗不理"
}'

4. 补充资料

~/.ssh/config 的妙用

何种场景下Elasticsearch会丢失数据?

Linux两台机器无需密码相互认证

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值