es docker 安装

安装过程如下:

首先在docker hub 拉取elk 镜像 (因为我用的是630 版本所以加了版本号)https://hub.docker.com/r/sebp/elk

关于容器更多信息查看 https://elk-docker.readthedocs.io/

docker pull sebp/elk:630 

启动命令( 如果想在后台启动容器的话可以 加 -d )

sudo docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -it --name elk sebp/elk

稍等片刻则启动成功 访问 http://localhost:5601 进入kibana

此时发现没有ik 插件so 安装 ik 插件

进入容器中es 的安装位置/opt/elasticsearch/bin/ 进入容器的命令如下

docker exec -it [es的容器id] /bin/bash

执行 安装ik 命令 (ik 需要对应es版本哦,可以 https://github.com/medcl/elasticsearch-analysis-ik/releases/ 在这里查找版本)

./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip

ik 安装下载后,需要重启容器

docker restart [es的容器id]

OK 此时已经安装成功了,你可以尽情的使用 ik 了
关于es和 spring boot 的结合使用 点击

es 多种查询

1. 新建mapping

新建学生的mapping
索引为 student_info
type 为student

PUT student_info
{
    "mappings" : {
      "student" : {
        "properties" : {
          "id" : {
            "type" : "keyword"
          },
           "id_card" : {
            "type" : "keyword"
          },
           "user_name" : {
            "type" : "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart",
            "fields": {
            "keyword": {
              "type": "keyword"
            }
           }
          },
          "mobile" : {
            "type" : "keyword"
          },
           "gender" : {
            "type" : "byte"
          },
          "age" : {
            "type" : "integer"
          },
           "height" : {
            "type" : "integer"
          },
           "weight" : {
            "type" : "float"
          },
          "update_time":{
             "type":"date"  ,
               "format": "yyyy-MM-dd HH:mm:ss"
          }
        }
      }
    }
  }
查询

此部分可参考文档:
https://www.cnblogs.com/ghj1976/p/5293250.html
https://blog.csdn.net/ZYC88888/article/details/82964474
https://blog.csdn.net/tanga842428/article/details/75127418

// 查询从第2条数据开始往后查询3条数据,也就是第3,4,5 条数据 用作分页
GET user_portrait_profiles/doc/_search
 {"from": 2,
  "size": 3
}

//should 是或
// must 是 且
// term主要用于精确匹配哪些值


range 过滤
range过滤允许我们按照指定范围查找一批数据:
{
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
}

范围操作符包含:

gt :: 大于
gte:: 大于等于
lt :: 小于
lte:: 小于等于

// bool 过滤
// bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

must :: 多个查询条件的完全匹配,相当于 and。
must_not :: 多个查询条件的相反匹配,相当于 not。
should :: 至少有一个查询条件匹配, 相当于 or。


一个 bool 过滤器由三部分组成:

{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
      "filter":    []
   }
}
must ——所有的语句都 必须(must) 匹配,与 AND 等价。
must_not ——所有的语句都 不能(must not) 匹配,与 NOT 等价。
should ——至少有一个语句要匹配,与 OR 等价。
filter——必须匹配,运行在非评分&过滤模式。

//例子
//分页 keyword 查询
{
  "_source": [ "account_id", "user_source","id_card","user_name","mobile","birth","sex","age","height","weight" ],
	"query": {
		"bool": {
			"should": [{
					"multi_match": {
						"fields": [
							"user_name.keyword", "user_name"
						],
						"query": "杨伊"
					}
				},
				{
					"term": {
						"id_card": "3110382918274"
					}
				},
				{
					"term": {
						"mobile": "15648220096"
					}
				}
			]
		}
	},
	"sort": [{
			"_score": {
				"order": "desc"
			}
		},
		{
			"mobile": {
				"order": "desc"
			}
		}

	],
	"from": 0,
	"size": 20
}


关于别名
## 设置别名
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "student_info_v_update",
        "alias": "student_info"
      }
    }
  ]
}

2:查询别名  student_info 或 别名
GET student_info/_alias/*

结果
{
  "student_info_v_update": {
    "aliases": {
      "student_info": {}
    }
  }
}

3、删除别名
POST /_aliases
{
    "actions": [
        {"remove": {"index": "student_info_v_update", "alias": "student_info"}}
    ]
}

4、修改别名
es没有修改别名的操作,只能先删除后添加
POST _aliases
{
  "actions" : [{"remove" : {"index" : "student_info_v_update" , "alias" : "student_info"}}],
  "actions" : [{"add" : {"index" : "student_info_v_update" , "alias" : "student_info"}}]
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐