侧边栏壁纸
  • 累计撰写 79 篇文章
  • 累计创建 7 个标签
  • 累计收到 0 条评论

ElasticSearch

水龙吟
2021-11-05 / 0 评论 / 0 点赞 / 199 阅读 / 8,491 字
温馨提示:
本文最后更新于 2021-12-27,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
  • 官网
https://www.elastic.co
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-create-index.html#java-rest-high-create-index-request

image.png

  • 启动保错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144],意思是elasticsearch的用户内存太小
vim /etc/sysctl.conf
添加配置:vm.max_map_count=262144
执行命令:/sbin/sysctl -p 立即生效



GET _search
{
  "query": {
    "match_all": {}
  }
}


GET /_analyze
{
  "analyzer": "ik_max_word",
  "text": "乒乓球明年总冠军"
}

GET /_analyze
{
  "analyzer": "ik_max_word",
  "text": "深圳南山区"
}

GET /_analyze
{
  "analyzer": "ik_smart",
  "text": "乒乓球明年总冠军"
}


GET /_analyze
{
  "analyzer": "standard",
  "text": "乒乓球明年总冠军"
}





#索引操作
PUT person

GET person

GET /blog1

DELETE person

# 查询映射
GET person/_mapping
GET blog1/_mapping
# 添加映射
PUT person/_mapping
{
  "properties":{
    "name":{
      "type":"keyword"
    },
    "age":{
      "type":"integer"
    }
  }
}

# ------------------------------------------------
# 创建索引并添加映射
PUT person
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "age":{
        "type":"integer"
      }
    }
  }
}


# 索引库中添加字段
PUT person/_mapping
{
  "properties":{
    "address":{
      "type":"text",
      "analyzer": "ik_max_word"
    }
  }
}

# 索引库中添加字段
PUT person/_mapping
{
  "properties":{
    "address1":{
      "type":"text"

    }
  }
}




# 添加文档,指定id
PUT person/_doc/1
{
  "name":"张三",
  "age":20,
  "address":"深圳宝安区",
  "address1":"深圳宝安区"
}


# 查询文档
GET person/_doc/1



# 添加文档,不指定id,会自动分配id
POST person/_doc/
{
  "name":"李四",
  "age":20,
  "address":"深圳南山区",
  "address1":"深圳宝安区"
}

# 查询文档
GET person/_doc/w-337XwB9vA-maKnuarV


# 查询所有文档
GET person/_search

# 删除文档
DELETE person/_doc/1


# 修改文档 根据id,id存在就是修改,id不存在就是添加
PUT person/_doc/2
{
  "name":"硅谷",
  "age":20,
  "address":"深圳福田保税区",
  "address1":"深圳宝安区"
}


#全文查询-match查询
# match 先会对查询的字符串进行分词,在查询,求交集
GET person/_search
{
  "query": {
    "match": {
      "address": "深圳保税区"
    }
  }
}


# 查询所有数据
GET person/_search
GET person/_mapping
# 查询 带某词条的数据
#词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时才匹配搜索,ik_max_word和standard有区别的,
#ik_max_word的倒排索引中有南山区,standard没有

GET person/_search
{
  "query": {
    "term": {
      "address": {
        "value": "南山区"
      }
    }
  }
}


GET person/_search
{
  "query": {
    "term": {
      "address1": {
        "value": "南山区"
      }
    }
  }
}


#关键字搜索数据
# 查询名字等于张三的用户
GET  person/_search?q=name:张三





PUT  shangguigu/_doc/1001
{
	"id":"1001",
	"name":"张三",
	"age":20,
	"sex":"男"
}

PUT  shangguigu/_doc/1002
{
	"id":"1002",
	"name":"李四",
	"age":25,
	"sex":"女"
}

PUT  shangguigu/_doc/1003
{
	"id":"1003",
	"name":"王五",
	"age":30,
	"sex":"女"
}

PUT  shangguigu/_doc/1004
{
	"id":"1004",
	"name":"赵六",
	"age":30,
	"sex":"男"
}

GET shangguigu/_search



POST  shangguigu/_doc/_search
{
  "query":{
      "match":{
          "age":20
      }
  }
}


#查询年龄大于20岁的女性用户
GET  shangguigu/_search
{
  "query":{
    "bool":{
      "filter":{
        "range":{
          "age":{
            "gt":20
          }
        }
      },
      "must":{
        "match":{
          "sex":"女"
        }
      }
    }
  }
}



GET  shangguigu/_search
{
    "query":{
       "match":{
           "name": "张三 李四"
       }
    }
}



#高亮
GET  shangguigu/_search
{
    "query":{
       "match":{
           "name": "张三 李四"
       }
    },
    "highlight": {
        "fields": {
            "name": {}
        }
    }
}

#没有指定映射会自动生成映射
GET shangguigu/_mapping
GET shangguigu



#聚合
#在Elasticsearch中,支持聚合操作,类似SQL中的group
#by操作。

GET  shangguigu/_search
{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "age"
            }
         }
    }
}

#text字段分组须要name.keyword
GET  shangguigu/_search
{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "name.keyword"
            }
         }
    }
}


#指定响应字段
GET  shangguigu/_doc/1001?_source=id,name
#等价于
GET /shangguigu/_search
{
  "query": {
    "match": {
      "id": "1001"
    }
  },
  "_source": ["id","name"]
}


#判断文档是否存在
HEAD  shangguigu/_doc/1001

#批量操作   mget通配符请求; 下载多个文件;
POST  shangguigu/_doc/_mget
{
  "ids" : [ "1001", "1003" ]
}

#如果,某一条数据不存在,不影响整体响应,需要通过found的值进行判断是否查询到数据。
POST  shangguigu/_doc/_mget
{
  "ids" : [ "1001", "10086" ]
}


#_bulk操作 在Elasticsearch中,支持批量的插入、修改、删除操作,都是通过_bulk的api完成的。
#请求格式如下:(请求格式不同寻常)

#批量插入数据
POST _bulk
{"create":{"_index":"atguigu","_id":2001}}
{"id":2001,"name":"name1","age": 20,"sex": "男"}
{"create":{"_index":"atguigu","_id":2002}}
{"id":2002,"name":"name2","age": 20,"sex": "男"}
{"create":{"_index":"atguigu","_id":2003}}
{"id":2003,"name":"name3","age": 20,"sex": "男"}


#更新
POST _bulk
{"update":{"_index":"atguigu","_id":2001}}
{"doc":{"id":2001,"name":"name1","age":21,"sex":"男"}}


GET atguigu/_mapping


POST _bulk
{"delete":{"_index":"atguigu","_id":2001}}
{"delete":{"_index":"atguigu","_id":2002}}
{"delete":{"_index":"atguigu","_id":2003}}

#分页
GET /_search?size=5
GET /_search?size=5&from=5
GET /_search?size=5&from=10
GET /_search?size=5&from=20

#指定索引分页
GET shangguigu/_search?size=4&from=4




POST atguigu/_bulk
{"index":{"_index":"atguigu"}}
{"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
{"index":{"_index":"atguigu"}}
{"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
{"index":{"_index":"atguigu"}}
{"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
{"index":{"_index":"atguigu"}}
{"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳"}
{"index":{"_index":"atguigu"}}
{"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影"}



POST atguigu/_search
{
  "query" : {
    "match" : { 
      "hobby" : "音乐 羽毛球"
    }
  },
  "from": 0,
  "size": 5
}







#terms
POST atguigu/_search
{
  "query" : {
      "terms" : { 
          "age" : [20,21]
    }
  }
}


#range
POST atguigu/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 22
      }
    }
  },
   "_source": ["id","name"]
}


POST atguigu/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 22
      }
    }
  }
}



# "exists":  必须包含
POST atguigu/_search
{
  "query": {
    "exists": { 
      "field": "mail"
    }
  }
}






















  • 重建索引

# 新建student_index_v1索引,索引名称必须全部小写
PUT student_index_v1 
{
  "mappings": {
    "properties": {
      "birthday":{
        "type": "date"
      }
    }
  }
}
# 查询索引
GET student_index_v1
# 添加数据
PUT student_index_v1/_doc/1
{
  "birthday":"2020-11-11"
}
# 查询数据
GET student_index_v1/_search
# 随着业务的变更,换种数据类型进行添加数据,程序会直接报错
PUT student_index_v1/_doc/1
{
  "birthday":"2020年11月11号"
}
# 业务变更,需要改变birthday数据类型为text
# 1:创建新的索引 student_index_v2
# 2:将student_index_v1 数据拷贝到 student_index_v2

# 创建新的索引
PUT student_index_v2 
{
  "mappings": {
    "properties": {
      "birthday":{
        "type": "text"
      }
    }
  }
}

DELETE student_index_v2

# 2:将student_index_v1 数据拷贝到 student_index_v2
POST _reindex
{
  "source": {
    "index": "student_index_v1"
  },
  "dest": {
    "index": "student_index_v2"
  }
}

# 查询新索引库数据
GET student_index_v2/_search

# 在新的索引库里面添加数据
PUT student_index_v2/_doc/2
{
  "birthday":"2020年11月13号"
}

0

评论区