elasticsearch

1、创建用户

es启动时需要使用非root用户,所以创建一个普通用户zephyr,安装es在目录/home/zephyr/apps下。

1.1 创建用户组hadoop

groupadd zephyr

1.2 创建用户hadoop并添加进用户组hadoop

useradd -d /home/zephyr -g zephyr -s /bin/bash -m zephyr
其含义如下:

-d, --home HOME_DIR
-g, --gid GROUP
-s, --shell SHELL
-m, --create-home

man useradd

1.3 添加sudoer权限

visudo
在文中root行下添加zephyr行
root	ALL=(ALL:ALL)	ALL
zephyr	ALL=(ALL:ALL)	ALL

其中:visudo <=> vi /etc/sudoers

1.4、设置密码

passwd zephyr  		给已创建的用户 zephyr 设置密码为hadoop  说明:新创建的用户会在/home下创建一个用户目录zephyr
usermod --help  	修改用户这个命令的相关参数
userdel zephyr  	删除用户zephyr

切换到zephyr
su - zephyr

2、安装jdk

(jdk要求1.8.20或1.7.55以上)

3 elasticsearch安装

3.1、上传

elasticsearch-2.3.1.tar.gz
上传es安装包 到 目录:/home/zephyr/apps

3.2、解压

tar -zxvf elasticsearch-2.3.1.tar.gz -C /bigdata/

3.3、修改配置

vi elasticsearch/config/elasticsearch.yml

#集群名称,通过组播的方式通信,通过名称判断属于哪个集群
cluster.name: lfzephyr
#节点名称,要唯一
node.name: es-1
#数据存放位置
path.data: /home/zephyr/data/es
#日志存放位置
path.logs: /home/zephyr/logs/es
#es绑定的ip地址
network.host: 192.168.18.13
#初始化时可进行选举的节点
discovery.zen.ping.unicast.hosts: ["mini3", "mini4", "mini5"]

3.4、使用scp拷贝到其他节点

scp -r elasticsearch/ mini4:$PWD
scp -r elasticsearch/ mini5:$PWD

3.5、在其他节点上修改es配置

需要修改的有 node.name 和 network.host

3.6、启动es

./elasticsearch/bin/elasticsearch -h	--查看帮助文档
./elasticsearch/bin/elasticsearch -d	--在后台启动

3.7、用浏览器访问es所在机器的9200端口

http://mini3:9200/

# 看到结果,表示ok

{
  "name" : "es-1",
  "cluster_name" : "lfzephyr",
  "version" : {
    "number" : "2.3.1",
    "build_hash" : "bd980929010aef404e7cb0843e61d0665269fc39",
    "build_timestamp" : "2016-04-04T12:25:05Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

kill `ps -ef | grep Elasticsearch | grep -v grep | awk '{print $2}'`

4.es安装插件

#下载es插件并安装-- 测试不ok
	./elasticsearch/bin/plugin install mobz/elasticsearch-head

#本地方式安装head插件  --上传插件并安装,测试ok
	./elasticsearch/bin/plugin install file:///home/zephyr/plugins/elasticsearch-head-master.zip

#访问head管理页面
	http://mini3:9200/_plugin/head

5.es的RESTful接口操作

RESTful接口URL的格式:
http://localhost:9200/<index>/<type>/[<id>]
其中index、type是必须提供的。
id是可选的,不提供es会自动生成。
index、type将信息进行分层,利于管理。
index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。

5.1 向store索引中添加一些书籍

curl -XPUT 'http://mini3:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'

5.2 通过浏览器查询

http://mini3:9200/store/books/1
http://mini4:9200/store/books/1
http://mini5:9200/store/books/1

5.3 在linux中通过curl的方式查询

curl -XGET 'http://mini3:9200/store/books/1'

5.4 在添加一个书的信息

curl -XPUT 'http://mini3:9200/store/books/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'

5.5 通过ID获得文档信息

curl -XGET 'http://mini3:9200/store/books/1'

5.6 在浏览器中查看

http://mini3:9200/store/books/1

5.7 通过_source获取指定的字段

curl -XGET 'http://mini3:9200/store/books/1?_source=title'
curl -XGET 'http://mini3:9200/store/books/1?_source=title,price'
curl -XGET 'http://mini3:9200/store/books/1?_source'

5.8 可以通过覆盖的方式更新

curl -XPUT 'http://mini3:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "lifeng",
    "last" : "wind"
  },
  "publish_date":"2020-02-26",
  "price":"199.99"
}'

5.9 或者通过 _update API的方式单独更新你想要更新的

curl -XPOST 'http://mini3:9200/store/books/1/_update' -d '{
  "doc": {
     "price" : 188.88
  }
}'

curl -XGET 'http://mini3:9200/store/books/1'

5.10 删除一个文档

curl -XDELETE 'http://mini3:9200/store/books/1'

5.11 最简单filter查询

SELECT * FROM books WHERE price = 35.99

# filtered 查询价格是35.99的
curl -XGET 'http://mini3:9200/store/books/_search' -d '{
    "query" : {
        "filtered" : {
            "query" : {
                "match_all" : {}
            },
            "filter" : {
                "term" : {
                    "price" : 34.99
                  }
              }
        }
    }
}'

# 指定多个值
curl -XGET 'http://mini3:9200/store/books/_search' -d '{
    "query" : {
        "filtered" : {
            "filter" : {
                "terms" : {
                    "price" : [35.99, 119.99]
                  }
              }
        }
    }
}'


# SELECT * FROM books WHERE publish_date = "2015-02-06"
curl -XGET 'http://mini3:9200/store/books/_search' -d '{
  "query" : {
    "filtered" : {
        "filter" : {
           "term" : {
              "publish_date" : "2015-02-06"
            }
          }
      }
  }
}'

5.12 bool过滤查询,可以做组合过滤查询

# SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND (publish_date != "2020-02-16")

# 类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式
# 格式如下:
#  {
#    "bool" : {
#    "must" :     [],
#    "should" :   [],
#    "must_not" : [],
#    }
#  }
#
# must: 条件必须满足,相当于 and
# should: 条件可以满足也可以不满足,相当于 or
# must_not: 条件不需要满足,相当于 not

curl -XGET 'http://mini3:9200/store/books/_search' -d '{
  "query" : {
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" : [
            { "term" : {"price" : 35.99}},
            { "term" : {"price" : 199.99}}
          ],
 "must_not" : {
            "term" : {"publish_date" : "2020-02-16"}
          }
        }
      }
    }
  }
}'

5.13 嵌套查询

# SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2020-02-16" AND price = 199.99 )

curl -XGET 'http://mini3:9200/store/books/_search' -d '{
  "query" : {
    "filtered" : {
      "filter" : {
        "bool" : {
          "should" : [
              { "term" : {"price" : 35.99}},
              { "bool" : {
              "must" : [
                {"term" : {"publish_date" : "2020-02-16"}},
                {"term" : {"price" : 199.99}}
              ]
            }}
          ]
        }
      }
    }
  }
}'

5.14 range范围过滤

# SELECT * FROM books WHERE price >= 20 AND price < 100
# gt :  > 大于
# lt :  < 小于
# gte :  >= 大于等于
# lte :  <= 小于等于

curl -XGET 'http://mini3:9200/store/books/_search' -d '{
  "query" : {
    "filtered" : {
      "filter" : {
        "range" : {
          "price" : {
            "gt" : 10.0,
            "lt" : 300
          }
        }
      }
    }
  }
}'

5.15 另外一种 and, or, not查询

# 没有bool, 直接使用and , or , not
# 注意: 不带bool的这种查询不能利用缓存
# 查询价格既是35.99,publish_date又为"2015-02-06"的结果

curl -XGET 'http://mini3:9200/store/books/_search' -d '{
  "query": {
    "filtered": {
      "filter": {
        "and": [
        {
          "term": {
            "price":119.99
          }
        },
        {
          "term": {
            "publish_date":"2020-02-16"
          }
        }
       ]
     },
     "query": {
      "match_all": {}
      }
    }
  }
}'

5.16 删除索引

curl -XDELETE http://mini3:9200/gamelog
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值