1:安装:先去官网下载下载地址: https://www.elastic.co/downloads/elasticsearch 。我去,这也太慢了,200多MB要下到什么时候?赶紧找一个国内的镜像站,换到华为镜像,目前版本是7.6.2,所以访问 https://mirrors.huaweicloud.com/elasticsearch/7.6.2/ 我下载的是zip包,下载完成后直接解压。 同样访问 https://mirrors.huaweicloud.com/kibana/7.6.2/ 下载kibana
2:启动:双击elasticsearch.bat启动服务,命令行窗口会显示相关的信息,默认端口为9200。确认 elasticsearch 正确启动 $ curl -X GET 'http://localhost:9200/' -H 'Content-Type: application/json'
。curl需要安装cygwin,安装时注意填入国内镜像站地址,参见 http://www.zhushiyao.com/?p=23392
设置用户名密码(暂时不用)$ curl -X GET -u "elastic:changeme" 'http://localhost:9200/' -H 'Content-Type: application/json
双击 kibana-7.6.2-windows-x86_64\bin\kibana.bat 启动kibana,默认地址是 http://localhost:5601 ,配置文件在: $KIBANA_HOME/config/kibana.yml
3:建立索引:
$curl -XPUT ‘http://localhost:9200/twitter/_doc/3?pretty’ -H ‘Content-Type: application/json’ -d ‘
{
“user”: “elastic”,
“post_date”: “2010-01-15T01:46:38”,
“message”: “Building the site, should be kewl”
}’
通过上面方法写入到Elasticsearch的文档,在默认的情况下并不马上可以进行搜索。这是因为在Elasticsearch的设计中,有一个叫做refresh的操作。它可以使更改可见以进行搜索的操作。通常会有一个refresh timer来定时完成这个操作。这个周期为1秒。
也可以在kibana的dev tool里执行,
Console UI 分成两个窗格:编辑器窗格(左)和响应窗格(右)。使用编辑器键入请求并将它们提交到 Elasticsearch。结果将显示在右侧的响应窗格中。
4:检查索引存在。
$ curl -X GET 'http://localhost:9200/twitter/_doc/3?pretty=true'
5:搜索。
$ curl -XGET 'http://localhost:9200/twitter/_search?q=user:kimchy&pretty=true'
#指定索引里搜索
$curl -XGET ‘http://localhost:9200/twitter/_search?pretty=true’ -H ‘Content-Type: application/json’ -d ‘
{
“query” : {
“match” : { “user”: “kimchy” }
}
}’
以下命令使用的是 kibana dev tool
查看Elasticsearch信息
GET /
创建
PUT twitter/_doc/1
{
“user”: “GB”,
“uid”: 1,
“city”: “Beijing”,
“province”: “Beijing”,
“country”: “China”
}
修改
POST twitter/_doc/1
{
“user”: “GB”,
“uid”: 1,
“city”: “Shenzhen”,
“province”: “Guangdong”,
“country”: “China”
}
防止覆盖,如果文档已经存在的话,我们会收到一个错误的信息
PUT twitter/_create/1
{
“user”: “GB”,
“uid”: 1,
“city”: “Shenzhen”,
“province”: “Guangdong”,
“country”: “China”
}
查看文档
GET twitter/_doc/1
只查看source的内容
GET twitter/_doc/1/_source
只查看部分字段
GET twitter/_doc/1?_source=city,country
查看多个文档
GET _mget
{
“docs”: [
{
“_index”: “twitter”,
“_id”: 1
},
{
“_index”: “twitter”,
“_id”: 2
}
]
}
查看多个文档
GET twitter/_doc/_mget
{
“ids”: [“1”, “2”]
}
更新文档
POST twitter/_update/1
{
“doc”: {
“city”: “成都”,
“province”: “四川”
}
}
先查询,后修改
POST twitter/_update_by_query
{
“query”: {
“match”: {
“user”: “GB”
}
},
“script”: {
“source”: “ctx._source.city = params.city;ctx._source.province = params.province;ctx._source.country = params.country”,
“lang”: “painless”,
“params”: {
“city”: “上海”,
“province”: “上海”,
“country”: “中国”
}
}
}
处理名字是中文字段的文档
POST twitter/_update_by_query
{
“query”: {
“match”: {
“姓名”: “张彬”
}
},
“script”: {
“source”: “ctx._source[\”签到状态\”] = params[\”签到状态\”]”,
“lang”: “painless”,
“params” : {
“签到状态”:”已签到”
}
}
}
直接根据id来更新
POST twitter/_update/1
{
“script” : {
“source”: “ctx._source.city=params.city”,
“lang”: “painless”,
“params”: {
“city”: “长沙”
}
}
}
更新或插入
POST twitter/_update/3
{
“doc”: {
“user”: “GB”,
“uid”: 3,
“city”: “常德”,
“province”: “湖南”,
“country”: “China”
},
“doc_as_upsert”: true
}
GET twitter/_doc/3
检查一个文档是否存在
HEAD twitter/_doc/1
删除一个文档
DELETE twitter/_doc/1
搜索后进行删除
POST twitter/_delete_by_query
{
“query”: {
“match”: {
“city”: “上海”
}
}
}
批量处理
POST _bulk
{ “index” : { “_index” : “twitter”, “_id”: 1} }
{“user”:”双榆树-张三”,”message”:”今儿天气不错啊,出去转转去”,”uid”:2,”age”:20,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市海淀区”,”location”:{“lat”:”39.970718″,”lon”:”116.325747″}}
{ “index” : { “_index” : “twitter”, “_id”: 2 }}
{“user”:”东城区-老刘”,”message”:”出发,下一站云南!”,”uid”:3,”age”:30,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市东城区台基厂三条3号”,”location”:{“lat”:”39.904313″,”lon”:”116.412754″}}
{ “index” : { “_index” : “twitter”, “_id”: 3} }
{“user”:”东城区-李四”,”message”:”happy birthday!”,”uid”:4,”age”:30,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市东城区”,”location”:{“lat”:”39.893801″,”lon”:”116.408986″}}
{ “index” : { “_index” : “twitter”, “_id”: 4} }
{“user”:”朝阳区-老贾”,”message”:”123,gogogo”,”uid”:5,”age”:35,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市朝阳区建国门”,”location”:{“lat”:”39.718256″,”lon”:”116.367910″}}
{ “index” : { “_index” : “twitter”, “_id”: 5} }
{“user”:”朝阳区-老王”,”message”:”Happy BirthDay My Friend!”,”uid”:6,”age”:50,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市朝阳区国贸”,”location”:{“lat”:”39.918256″,”lon”:”116.467910″}}
{ “index” : { “_index” : “twitter”, “_id”: 6} }
{“user”:”虹桥-老吴”,”message”:”好友来了都今天我生日,好友来了,什么 birthday happy 就成!”,”uid”:7,”age”:90,”city”:”上海”,”province”:”上海”,”country”:”中国”,”address”:”中国上海市闵行区”,”location”:{“lat”:”31.175927″,”lon”:”121.383328″}}
查询到所有的输入的文档
POST twitter/_search
查询有多少条数据
GET twitter/_count
批量操作,index和create的区别。index总是可以成功,它可以覆盖之前的已经创建文档,但是create则不行,如果已经有以那个id为名义的文档,就不会成功
POST _bulk
{ “create” : { “_index” : “twitter”, “_id”: 1} }
{“user”:”双榆树-张三”,”message”:”今儿天气不错啊,出去转转去”,”uid”:2,”age”:20,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市海淀区”,”location”:{“lat”:”39.970718″,”lon”:”116.325747″}}
{ “index” : { “_index” : “twitter”, “_id”: 2 }}
{“user”:”东城区-老刘”,”message”:”出发,下一站云南!”,”uid”:3,”age”:30,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市东城区台基厂三条3号”,”location”:{“lat”:”39.904313″,”lon”:”116.412754″}}
{ “index” : { “_index” : “twitter”, “_id”: 3} }
{“user”:”东城区-李四”,”message”:”happy birthday!”,”uid”:4,”age”:30,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市东城区”,”location”:{“lat”:”39.893801″,”lon”:”116.408986″}}
{ “index” : { “_index” : “twitter”, “_id”: 4} }
{“user”:”朝阳区-老贾”,”message”:”123,gogogo”,”uid”:5,”age”:35,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市朝阳区建国门”,”location”:{“lat”:”39.718256″,”lon”:”116.367910″}}
{ “index” : { “_index” : “twitter”, “_id”: 5} }
{“user”:”朝阳区-老王”,”message”:”Happy BirthDay My Friend!”,”uid”:6,”age”:50,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市朝阳区国贸”,”location”:{“lat”:”39.918256″,”lon”:”116.467910″}}
{ “index” : { “_index” : “twitter”, “_id”: 6} }
{“user”:”虹桥-老吴”,”message”:”好友来了都今天我生日,好友来了,什么 birthday happy 就成!”,”uid”:7,”age”:90,”city”:”上海”,”province”:”上海”,”country”:”中国”,”address”:”中国上海市闵行区”,”location”:{“lat”:”31.175927″,”lon”:”121.383328″}}
用delete来删除一个已经创建好的文档
POST _bulk
{ “delete” : { “_index” : “twitter”, “_id”: 1 }}
用update来进行更新一个文档
POST _bulk
{ “update” : { “_index” : “twitter”, “_id”: 2 }}
{“doc”: { “city”: “长沙”}}
关闭一个index
POST twitter/_close
打开一个index
POST twitter/_open
冻结索引
POST twitter/_freeze
包含冻结索引做搜索
POST twitter/_search?ignore_throttled=false
解冻索引
POST twitter/_unfreeze
删除一个index
DELETE twitter
对于那些名字是中文字段的文档来说,在painless语言中,直接打入中文字段名字,并不能被认可
搜索所有的文档
GET /_search
GET /_all/_search
设定返回数(默认10个)
GET /_search?size=20
对多个index进行搜索
POST /index1,index2,index3/_search
对所有以index为开头的索引来进行搜索,排除index3
POST /index*,-index3/_search
搜索特定的index
GET twitter/_search
分页
GET twitter/_search?size=2&from=2
GET twitter/_search
{
“size”: 2,
“from”: 2,
“query”: {
“match_all”: {}
}
}
通过filter_path来控制输出的较少的字段
GET twitter/_search?filter_path=hits.total
通过_source来定义返回想要的字段
GET twitter/_search
{
“_source”: [“user”, “city”],
“query”: {
“match_all”: {
}
}
}
设置_source为false,这样不返回任何的_source信息
GET twitter/_search
{
“_source”: false,
“query”: {
“match”: {
“user”: “张三”
}
}
}
用script field来生成在_source里没有的field
GET twitter/_search
{
“query”: {
“match_all”: {}
},
“script_fields”: {
“years_to_100”: {
“script”: {
“lang”: “painless”,
“source”: “100-doc[‘age’].value”
}
},
“year_of_birth”:{
“script”: “2019 – doc[‘age’].value”
}
}
}
查询我们的索引里到底有多少文档
GET twitter/_count
满足条件的文档的数量
GET twitter/_count
{
“query”: {
“match”: {
“city”: “北京”
}
}
}
获得一个index的settings
GET twitter/_settings
设置
PUT twitter
{
“settings”: {
“number_of_shards”: 1,
“number_of_replicas”: 1
}
}
查询目前的index的mapping
GET twitter/_mapping
正确地创建我们的mapping,我们必须先把之前的twitter索引删除掉,并同时使用settings来创建这个index
删除索引
DELETE twitter
PUT twitter
{
“settings”: {
“number_of_shards”: 1,
“number_of_replicas”: 1
}
}
创建mapping
PUT twitter/_mapping
{
“properties”: {
“address”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“age”: {
“type”: “long”
},
“city”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“country”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“location”: {
“type”: “geo_point”
},
“message”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“province”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
},
“uid”: {
“type”: “long”
},
“user”: {
“type”: “text”,
“fields”: {
“keyword”: {
“type”: “keyword”,
“ignore_above”: 256
}
}
}
}
}
查看 mapping
GET twitter/_mapping
批量导入
POST _bulk
{ “index” : { “_index” : “twitter”, “_id”: 1} }
{“user”:”双榆树-张三”,”message”:”今儿天气不错啊,出去转转去”,”uid”:2,”age”:20,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市海淀区”,”location”:{“lat”:”39.970718″,”lon”:”116.325747″}}
{ “index” : { “_index” : “twitter”, “_id”: 2 }}
{“user”:”东城区-老刘”,”message”:”出发,下一站云南!”,”uid”:3,”age”:30,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市东城区台基厂三条3号”,”location”:{“lat”:”39.904313″,”lon”:”116.412754″}}
{ “index” : { “_index” : “twitter”, “_id”: 3} }
{“user”:”东城区-李四”,”message”:”happy birthday!”,”uid”:4,”age”:30,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市东城区”,”location”:{“lat”:”39.893801″,”lon”:”116.408986″}}
{ “index” : { “_index” : “twitter”, “_id”: 4} }
{“user”:”朝阳区-老贾”,”message”:”123,gogogo”,”uid”:5,”age”:35,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市朝阳区建国门”,”location”:{“lat”:”39.718256″,”lon”:”116.367910″}}
{ “index” : { “_index” : “twitter”, “_id”: 5} }
{“user”:”朝阳区-老王”,”message”:”Happy BirthDay My Friend!”,”uid”:6,”age”:50,”city”:”北京”,”province”:”北京”,”country”:”中国”,”address”:”中国北京市朝阳区国贸”,”location”:{“lat”:”39.918256″,”lon”:”116.467910″}}
{ “index” : { “_index” : “twitter”, “_id”: 6} }
{“user”:”虹桥-老吴”,”message”:”好友来了都今天我生日,好友来了,什么 birthday happy 就成!”,”uid”:7,”age”:90,”city”:”上海”,”province”:”上海”,”country”:”中国”,”address”:”中国上海市闵行区”,”location”:{“lat”:”31.175927″,”lon”:”121.383328″}}
查询数据 match query
GET twitter/_search
{
“query”: {
“match”: {
“city”: “北京”
}
}
}
script search 比较低效
GET twitter/_search
{
“query”: {
“script”: {
“script”: {
“source”: “doc[‘city’].contains(params.name)”,
“lang”: “painless”,
“params”: {
“name”: “北京”
}
}
}
}
}
URI search
GET twitter/_search?q=city:”北京”
用filter来过滤
GET twitter/_search
{
“query”: {
“bool”: {
“filter”: {
“term”: {
“city.keyword”: “北京”
}
}
}
}
}
term 查询
GET twitter/_search
{
“query”: {
“constant_score”: {
“filter”: {
“term”: {
“city”: {
“value”: “北京”
}
}
}
}
}
}
指明一个专有的field来进行搜索
GET twitter/_search
{
“query”: {
“match”: {
“user”: {
“query”: “朝阳区-老贾”,
“operator”: “or”
}
}
}
}
GET twitter/_search
{
“query”: {
“match”: {
“user”: “朝阳区-老贾”
}
}
}
至少匹配3个字
GET twitter/_search
{
“query”: {
“match”: {
“user”: {
“query”: “朝阳区-老贾”,
“operator”: “or”,
“minimum_should_match”: 3
}
}
}
}
“and“操作
GET twitter/_search
{
“query”: {
“match”: {
“user”: {
“query”: “朝阳区-老贾”,
“operator”: “and”
}
}
}
}
multi_match搜索
GET twitter/_search
{
“query”: {
“multi_match”: {
“query”: “朝阳”,
“fields”: [
“user”,
“address^3”, //对address含有 “朝阳” 的文档的分数进行3倍的加权
“message”
],
“type”: “best_fields”
}
}
}
包含特定前缀的文档 Prefix query
GET twitter/_search
{
“query”: {
“prefix”: {
“user”: {
“value”: “朝”
}
}
}
}
精确的字词匹配 Term query
GET twitter/_search
{
“query”: {
“term”: {
“user.keyword”: {
“value”: “朝阳区-老贾”
}
}
}
}
多个terms进行查询
GET twitter/_search
{
“query”: {
“terms”: {
“user.keyword”: [
“双榆树-张三”,
“东城区-老刘”
]
}
}
}
复合查询(compound query)
GET twitter/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“city”: “北京”
}
},
{
“match”: {
“age”: “30”
}
}
]
}
}
}
把一些满足条件的排出在外,使用must_not
GET twitter/_search
{
“query”: {
“bool”: {
“must_not”: [
{
“match”: {
“city”: “北京”
}
}
]
}
}
}
should。它表述“或”的意思,也就是有就更好,没有就算了
GET twitter/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“age”: “30”
}
}
],
“should”: [
{
“match_phrase”: {
“message”: “Happy birthday”
}
}
]
}
}
}
位置查询
GET twitter/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“address”: “北京”
}
}
]
}
},
“post_filter”: {
“geo_distance”: {
“distance”: “3km”,
“location”: {
“lat”: 39.920086,
“lon”: 116.454182
}
}
}
}
在5公里以内的所有位置信息,并按照远近大小进行排序 sort
GET twitter/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“address”: “北京”
}
}
]
}
},
“post_filter”: {
“geo_distance”: {
“distance”: “5km”,
“location”: {
“lat”: 39.920086,
“lon”: 116.454182
}
}
},
“sort”: [
{
“_geo_distance”: {
“location”: “39.920086,116.454182”,
“order”: “asc”,
“unit”: “km”
}
}
]
}
范围查询
GET twitter/_search
{
“query”: {
“range”: {
“age”: {
“gte”: 30,
“lte”: 40
}
}
}
}
exists来查询一个字段是否存在
GET twitter/_search
{
“query”: {
“exists”: {
“field”: “city”
}
}
}
//准备的数据
PUT twitter/_doc/20
{
“user” : “王二”,
“message” : “今儿天气不错啊,出去转转去”,
“uid” : 20,
“age” : 40,
“province” : “北京”,
“country” : “中国”,
“address” : “中国北京市海淀区”,
“location” : {
“lat” : “39.970718”,
“lon” : “116.325747”
}
}
匹配短语
GET twitter/_search
{
“query”: {
“match”: {
“message”: “happy birthday”
}
}
}
match_phrase。它要求Happy必须是在birthday的前面
GET twitter/_search
{
“query”: {
“match_phrase”: {
“message”: “Happy birthday”
}
},
“highlight”: {
“fields”: {
“message”: {}
}
}
}
POST _bulk
{“index”:{“_index”:”twitter1″,”_id”:1}}
{“user”:”张庆”,”message”:”今儿天气不错啊,出去转转去”,”uid”:2,”age”:20,”city”:”重庆”,”province”:”重庆”,”country”:”中国”,”address”:”中国重庆地区”,”location”:{“lat”:”39.970718″,”lon”:”116.325747″}}
Multi Search API,使用单个API请求执行几次搜索
GET twitter/_msearch
{“index”:”twitter”}
{“query”:{“match_all”:{}},”from”:0,”size”:1}
{“index”:”twitter”}
{“query”:{“bool”:{“filter”:{“term”:{“city.keyword”:”北京”}}}}, “size”:1}
{“index”:”twitter1″}
{“query”:{“match_all”:{}}}
指定多个索引操作
GET twitter*/_search