ES安装
https://www.elastic.co/cn/downloads/elasticsearch
1.下载好对应的文件压缩包
2.解压以后 在bin目录下打开elasticsearch.bat 即可启动(注意:启动es会吃很多内存,内存不够需要修改就找到config文件目录下的jvm.options,打开找到(Xms:代表最小2G,Xmx代表最大2G),修改最小为200m,运行内存会变小。)
3.验证:访问:http://localhost:9200 默认端口
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-7.3.2.jar:7.3.2]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.3.2.jar:7.3.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.3.2.jar:7.3.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.3.2.jar:7.3.2]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.3.2.jar:7.3.2]
... 6 more
报这个错误说明不能以root用户启动
启动成功后,使用浏览器或者curl命令可以看到如下信息
{
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "SimUAqM4SqyzMWRd7nJQhQ",
"version" : {
"number" : "7.3.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "1c1faf1",
"build_date" : "2019-09-06T14:40:30.409026Z",
"build_snapshot" : false,
"lucene_version" : "8.1.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
elasticsearch配置
属性名 | 说明 |
---|---|
cluster.name | 配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称。 |
node.name | 节点名,es会默认随机指定一个名字,建议指定一个有意义的名称,方便管理 |
path.conf | 设置配置文件的存储路径,tar或zip包安装默认在es根目录下的config文件夹,rpm安装默认在/etc/ elasticsearch |
path.data | 设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开 |
path.logs | 设置日志文件的存储路径,默认是es根目录下的logs文件夹 |
path.plugins | 设置插件的存放路径,默认是es根目录下的plugins文件夹 |
bootstrap.memory_lock | 设置为true可以锁住ES使用的内存,避免内存进行swap |
network.host | 设置bind_host和publish_host,设置为0.0.0.0允许外网访问 |
http.port | 设置对外服务的http端口,默认为9200。 |
transport.tcp.port | 集群结点之间通信端口 |
discovery.zen.ping.timeout | 设置ES自动发现节点连接超时的时间,默认为3秒,如果网络延迟高可设置大些 |
discovery.zen.minimum_master_nodes | 主结点数量的最少值 ,此值的公式为:(master_eligible_nodes / 2) + 1 ,比如:有3个符合要求的主结点,那么这里要设置为2 |
建议:9300:集群节点间通讯接口
9200:客户端访问接口
使用入门
本质上也是储存数据 所以许多概念与Mysql类似
索引(indices)---------------数据库
文档(Document)------------Row 行
字段(Field)-------------------Columns 列
属性名 | 说明
---|---
索引库(indices)|indices是index的复数,代表许多的索引,
文档(document)|存入索引库原始的数据。比如每一条商品信息,就是一个文档
字段(field)|文档中的属性
映射配置(mappings)|字段的数据类型、属性、是否索引、是否存储等特性
---
要注意的是:Elasticsearch本身就是分布式的,因此即便你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。
创建索引库(数据库)
=="number_of_shards"==: 1 分片数
=="number_of_replicas"==: 1 副本数
PUT people
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
返回结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "people"
}
删索引库
GET people
结果:
{
"people" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1564308107803",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "zQYt9-BXR9yuQOLNugAabg",
"version" : {
"created" : "7020099"
},
"provided_name" : "item"
}
}
}
}
查询索引库是否存在
HEAD people
返回
200 - OK
创建映射关系(数据库列)
- type:类型,可以是text、long、short、date、integer、object等
- index:是否索引,默认为true
- store:是否存储,默认为false
- analyzer:分词器,这里的ik_max_word即使用ik分词器
PUT people/_mapping
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"age": {
"type": "integer",
"index": "false"
},
"sex": {
"type": "keyword"
}
}
}
如果再次创建不通话映射则为新增映射
查询映射关系
GET people/_mapping
返回结果
{
"people" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "integer",
"index" : false
},
"name" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"sex" : {
"type" : "keyword"
}
}
}
}
}
添加 更新数据
我们也可以直接添加数据 这样系统会给我们默认生成一套映射关系
如果_doc 后面不指定id数 会默认生成随机id
POST people/_doc/1
{
"name" : "小明",
"age" : 18,
"sex": "男"
}
使用相同的代码更改部分 即可更改
POST people/_doc/1
{
"name" : "小红",
"age" : 18,
"sex": "女"
}
通过查询获取id 然后更新即可
获取数据
GET people/_doc/1
或者查询所有
GET people/_search
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "小红",
"age" : 18,
"sex" : "女"
}
}
删除数据
通过查询id进行删除即可
DELETE people/_doc/1
查询功能
我们添加小王2 和小男
POST people/_doc/2
{
"name" : "小王2",
"age" : 22,
"sex": "男"
}
POST people/_doc/3
{
"name" : "小男",
"age" : 22,
"sex": "男"
}
全部查询
基本语法
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
返回结果
time_out:是否超时
_shards:分片信息
hits:搜索结果总览对象
total:搜索到的总条数
max_score:所有结果中文档得分的最高分
hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
_index:索引库
_type:文档类型
_id:文档id
_score:文档得分
_source:文档的源数据
默认查询所有的people下的数据
GET people/_search
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "小红",
"age" : 18,
"sex" : "女"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "-idVPGwBetwbYuu2n6UB",
"_score" : 1.0,
"_source" : {
"name" : "小王",
"age" : 22,
"sex" : "男"
}
}
]
}
}
查询所有的people下的数据
GET people/_search
{
"query": {
"match_all": {}
}
}
匹配查询
查询name是小王2的字段 默认情况为or (即包含小王字段的也会查询到)
GET people/_search
{
"query": {
"match": {
"name": "小王2"
}
}
}
返回结果(部分)
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "-idVPGwBetwbYuu2n6UB",
"_score" : 0.52354836,
"_source" : {
"name" : "小王",
"age" : 22,
"sex" : "男"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.39019167,
"_source" : {
"name" : "小王2",
"age" : 22,
"sex" : "男"
}
}
]
如果我们只想要小王2 则通过更改关联关系(此时我们只能查询到小王2)
GET people/_search
{
"query": {
"match": {
"name":{"query": "小王2","operator": "and"}
}
}
}
设置最小匹配度查询
如果满足其中的50% 岂可视为查询正确 小王和小王2都会查询出来
如果设置为100% 则只会查出小王2
GET people/_search
{
"query": {
"match": {
"name":{"query": "小王2","minimum_should_match": "50%"}
}
}
}
多字段查询
我们在sex 和name中查询带 “男”字 的信息
GET people/_search
{
"query": {
"multi_match": {
"query": "男"
, "fields": ["name","sex"]
}
}
}
返回结果
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "-idVPGwBetwbYuu2n6UB",
"_score" : 0.2876821,
"_source" : {
"name" : "小王",
"age" : 22,
"sex" : "男"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "小王2",
"age" : 22,
"sex" : "男"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "小男",
"age" : 22,
"sex" : "男"
}
}
]
词条匹配(精确查找)
term 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串
terms 就是查询多个
GET people/_search
{
"query": {
"terms": {
"sex": [
"男","女"
]
}
}
结果(部分)
"hits" : [
{
"_index" : "people",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "小红",
"age" : 18,
"sex" : "女"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "-idVPGwBetwbYuu2n6UB",
"_score" : 1.0,
"_source" : {
"name" : "小王",
"age" : 22,
"sex" : "男"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "小王2",
"age" : 22,
"sex" : "男"
}
},
{
"_index" : "people",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "小男",
"age" : 22,
"sex" : "男"
}
}
]
结果过滤
我们只需要name字段
GET people/_search
{
"query": {
"terms": {
"sex": [
"男","女"
]
}
},"_source": "name"
}
我们不需要带有age 的信息
GET people/_search
{
"query": {
"terms": {
"sex": [
"男","女"
]
}
},"_source": {
"excludes": "age"
}
}
我们只需要性别和名字
GET people/_search
{
"query": {
"terms": {
"sex": [
"男","女"
]
}
},"_source": {
"includes": ["name","sex"]
}
}
返回结果(部分)"hits" : [ { "_index" : "people", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "sex" : "女", "name" : "小红" } }, { "_index" : "people", "_type" : "_doc", "_id" : "-idVPGwBetwbYuu2n6UB", "_score" : 1.0, "_source" : { "sex" : "男", "name" : "小王" } }, { "_index" : "people", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "sex" : "男", "name" : "小王2" } }, { "_index" : "people", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "sex" : "男", "name" : "小男" } } ]