es5.x 到7.x 的版本变动,最大的变动是type的变动了

5.x 支持多种type    6.x 只能有一种type   7.x 将去除type 没有类型的概念了

官网文档:https://www.elastic.co/guide/en/elasticsearch/reference

EVN :ubuntu16.04  

##################       version   ES7.15.1                  ######################
##################     ---by Sea 2021-11-03           ######################

1.下在ik分词器插件
    地址: https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.15.1/elasticsearch-analysis-ik-7.15.1.zip
         
    解压到:/opt/docker/elasticsearch/plugins/ik/   (zip包里面的所有文件)



.运行elasticsearch

      sudo docker run -itd  --restart=always  --name=es_sigle --net=host -p 9200:9200 -p 9300:9300 \ 
         --ulimit nofile=102400:102400 \
         -e "http.cors.enabled=true" \
         -e 'http.cors.allow-origin="*"' \
         -e "discovery.type=single-node" \
         -e node.name=es01  \
         -e bootstrap.memory_lock=true \
         --ulimit memlock = -1:-1  \
         -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
         -v /opt/docker/elasticsearch/data:/usr/share/elasticsearch/data:rw  \
         -v /opt/docker/elasticsearch/logs:/usr/share/elasticsearch/logs:rw \
      -v /opt/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins:rw \
         docker.elastic.co/elasticsearch/elasticsearch:7.15.1



.检查es是否启动
     3.1浏览器访问测试是否正常(以下为正常)
    # curl http://127.0.0.1:9200/
    {
    "name": "es01",
    "cluster_name": "my-application",
    "cluster_uuid": "0l6-CVNFQsyyBq80IqgLOA",
    "version": {
    "number": "7.15.1",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "83c34f456ae29d60e94d886e455e6a3409bba9ed",
    "build_date": "2021-10-07T21:56:19.031608185Z",
    "build_snapshot": false,
    "lucene_version": "8.9.0",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
    },
    "tagline": "You Know, for Search"
    }


    3.2 安装elasticsearch head  :https://www.cnblogs.com/lshan/p/14120654.html


.基本使用:
   4.1 创建index (bxuser)
       curl -XPUT http://localhost:9200/bxuser
   
   4.2 创建 mapping

    curl -XPOST http://localhost:9200/bxuser/_mapping -H 'Content-Type:application/json' -d'
    {
        "properties": 
        {
            "remark": 
            {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            },
            "name": 
            {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            },
            "address": 
            {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }

    }'
    
  
5.插入几条数据
       
    curl -XPOST http://localhost:9200/bxuser/_create/1 -H 'Content-Type:application/json' -d'
    {"remark":"sea武力展示?印度陆军伞兵旅在中印边境地区开展空降演习","name":"sea","address":"china  gongdong  shengzhen futian","email":"lshan523@163.com"}
    '

    curl -XPOST http://localhost:9200/bxuser/_create/2 -H 'Content-Type:application/json' -d'
    {"remark":"Sea将在开幕式上发表主旨演讲","name":"Sea","address":"台湾的前途只有一个选项 那就是两岸统一、民族复兴","email":"sea@mail.icil.cn"}
    '

    curl -XPOST http://localhost:9200/bxuser/_create/3 -H 'Content-Type:application/json' -d'
    {"remark":"拜登约会岸田文雄 两人亲昵碰肘确认强化美日同盟","name":"拜登","address":"日本新增病例骤减,未必是病毒自我消亡!","email":"sea@mail.icil.cn"}
    '

    curl -XPOST http://localhost:9200/bxuser/_create/4 -H 'Content-Type:application/json' -d'
    {"remark":"sea G20罗马峰会  ","name":"sea","address":"sea 重要主张广受赞誉  暖心瞬间","email":"lshan523@163.com"}
    '



6  查询并高亮显示

    curl -XPOST http://localhost:9200/bxuser/_search  -H 'Content-Type:application/json' -d'
    {
        "query" : { "match" : { "remark" : "拜登 sea" }},
        "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
        }
    }
    '

 

 

二 常用操作:

1) 修改mapping(新增)   :

     官网:https://www.elastic.co/guide/en/elasticsearch/reference/7.15/indices-put-mapping.html

      mapping是不支持删除属性的, 只能新增 ()

  首先,创建一条index并指定mapping

curl -XPUT http://localhost:9200/mapping_update -H 'Content-Type:application/json' -d'
{
    "mappings":{
        "properties":{
            "name":{"type":"text"}
        }
    }
}'

#创建成功,我们查看一下: 
 curl -X GET localhost:9200/mapping_update?pretty  

#修改mapping新增一条属性age
curl -XPUT http://localhost:9200/mapping_update -H 'Content-Type:application/json' -d'
{
    "mappings":{
        "properties":{
            "age":{"type":"text"}
        }
    }
}'

#创建成功, 再次查看一下(mapping 增加了age): 
curl GET localhost:9200/mapping_update?pretty  

 2) 完全修改mapping   (方案来自)    原文:https://www.iteye.com/blog/donlianli-1924721

Elasticsearch的mapping一旦创建,只能增加字段,而不能修改已经mapping的字段。但现实往往并非如此啊,有时增加一个字段,就好像打了一个补丁,一个可以,但是越补越多,最后自己都觉得惨不忍睹了。怎么办??

这里有一个方法修改mapping,那就是重新建立一个index,然后创建一个新的mapping。你可能会问,这要是在生产环境,可行吗?答案是,如果你一开始就采取了合适的设计,这个完全是可以做到平滑过渡的。

采取什么合理设计呢?就是我们的程序访问索引库时,始终使用同义词来访问,而不要使用真正的indexName。在reindex完数据之后,修改之前的同义词即可。明白了吗?

参考上面的思路,我们来一步一步做。

step1、创建一个索引,这个索引的名称最好带上版本号,比如my_index_v1,my_index_v2等。

step2、创建一个指向本索引的同义词。

curl -XPOST localhost:9200/_aliases  -d '  
{  
    "actions": [  
        { "add": {  
            "alias": "my_index",  
            "index": "my_index_v1"  
        }}  
    ]  
}  
'  

 此时,你可以通过同义词my_index访问。包括创建索引,删除索引等。

step3,需求来了,需要更改mapping了,此时,你需要创建一个新的索引,比如名称叫my_index_v2(版本升级).,在这个索引里面创建你新的mapping结构。然后,将新的数据刷入新的index里面。在刷数据的过程中,你可能想到直接从老的index中取出数据,然后更改一下格式即可。如何遍历所有的老的index数据,请参考这里

step4,修改同义词。将指向v1的同义词,修改为指向v2。http接口如下:

curl -XPOST localhost:9200/_aliases -d '  
{  
    "actions": [  
        { "remove": {  
            "alias": "my_index",  
            "index": "my_index_v1"  
        }},  
        { "add": {  
            "alias": "my_index",  
            "index": "my_index_v2"  
        }}  
    ]  
}  
'  

step5,删除老的索引。

curl -XDELETE localhost:9200/my_index_v1  

 

posted on 2021-11-03 11:53  lshan  阅读(231)  评论(0编辑  收藏  举报