ElasticSearch学习(二)字段类型修改新增方案模糊查询

目录:场景一:修改 ElasticSearch 的字段(Field)的类型

           场景二:添加/新增 ElasticSearch 的字段(Field)

           场景三:ElasticSearch 根据某个字段模糊查询


场景一:需要修改ElasticSearch的字段(Field)的类型

分析:ElasticSearch是不支持修改字段(Field)的类型,不像mysql一个可以修改字段(Column)的数据类型,所有我们需要其他的思路来解决这个需求

解决方法:假设我们已经有了一个索引(Index)名称为:test 我们需要修改字段(Field)name(现在为“text”类型) 的类型为“keyword”类型,那么需要一下操作:

  • 1:新建一个索引(Index)名称为:test_v2,将字段(Field)name 的类型设置为“keyword”类型。
  • 2:将原来索引test的数据复制到test_v2中。
  • 3:删除索引test
  • 4:新建一个新的索引(Index)名称为:test,并且将字段(Field)name 的类型设置为“keyword”类型。
  • 5:将原来索引test_v2的数据复制到test中。
  • 6:删除索引test_v2。至此字段修改已经完成了

实践一下:

现在test的索引类型如下:知识点:查看索引的mapping结构

GET /test/_mapping
#返回信息
{
  "test": {
    "mappings": {
      "user": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "createDate": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "id": {
            "type": "keyword"
          },
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

1:新建一个索引(Index)名称为:test_v2,将字段(Field)name 的类型设置为“keyword”类型。知识点:创建索引

PUT test_v2
{
   "mappings": {
      "user": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "createDate": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "id": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        }
      }
    }
}

2:将原来索引test的数据复制到test_v2中。知识点:复制索引数据到另个一个索引里

POST _reindex
{
  "source": {
    "index": "test"
  },
  "dest": {
    "index": "test_v2"
  }
}

3:删除索引test。知识点:删除索引

DELETE /test

4:新建一个新的索引(Index)名称为:test,并且将字段(Field)name 的类型设置为“keyword”类型。

PUT test
{
   "mappings": {
      "user": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "createDate": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "id": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        }
      }
    }
}

5:将原来索引test_v2的数据复制到test中。

POST _reindex
{
  "source": {
    "index": "test_v2"
  },
  "dest": {
    "index": "test"
  }
}

6:删除索引test_v2。

DELETE /test_v2

注意:如果ElasticSearch的索引使用了别名可以在“步骤3”完成后做一下操作即可

 查看索引—别名映射

GET _cat/aliases
#返回信息如下
test_aliases                       test                    - - -

第一列是索引别名,第二列是索引名称

“步骤3”完成后,将test_v2索引映射到索引别名test_aliases即可

POST _aliases
{
  "actions": [
    {
      "add": {
        "alias": "test_aliases",
        "index": "test_v2"
      }
    }
  ]
}

场景二:添加/新增ElasticSearch的字段(Field)

分析:如果ElasticSearch字段已经建好,但是需要添加字段怎么办呢,ElasticSearch是支持添加字段的,但是添加后之前的数据是没有这个字段的,之后新增的或者修改后添加了这个字段才会显示

基本语法:

PUT /{index}/_mapping/{type}
{
  "{type}": {
    "properties": {
      "{filedName1}": {
        "type": "keyword"
      }, "{filedName2}": {
        "type": "text"
      }
    }
  }
}

实践:向test_v2索引新增togs字段

PUT /test_v2/_mapping/user
{
  "user": {
    "properties": {
      "tags": {
        "type": "keyword"
      } 
    }
  }
}

场景三:ElasticSearch根据某个字段模糊查询

分析:ElasticSearch 的字段类型为“keyword” 是支持模糊查询,就像mysql的select * from table like '%孩%' 一样

基本语法:

GET /{index}/_search
{
   "query": {
     "wildcard": {
       "FIELD": {
         "value": "VALUE"
       }
     }
   }
}
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "5",
        "_score": 1,
        "_source": {
          "id": "5",
          "name": "我们是祖国的花朵",
          "age": 30,
          "createDate": "2021-03-01 09:03:04"
        }
      },
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": "2",
          "name": "rose",
          "age": 19,
          "createDate": "2021-03-01 09:03:04"
        }
      },
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "4",
        "_score": 1,
        "_source": {
          "id": "4",
          "name": "我们是孩子",
          "age": 30,
          "createDate": "2021-03-01 09:03:04"
        }
      },
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": "2",
          "name": "rose",
          "age": 30,
          "createDate": "2021-03-01 09:03:04"
        }
      },
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "id": "3",
          "name": "我们都是好孩子",
          "age": 30,
          "createDate": "2021-03-01 09:03:04"
        }
      }
    ]
  }
}

上面是基础数据,我们来实践一下:

GET /test_v2/_search
{
   "query": {
     "wildcard": {
       "name": {
         "value": "孩"
       }
     }
   }
}
返回结果:
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "4",
        "_score": 1,
        "_source": {
          "id": "4",
          "name": "我们是孩子",
          "age": 30,
          "createDate": "2021-03-01 09:03:04"
        }
      },
      {
        "_index": "test_v2",
        "_type": "user",
        "_id": "3",
        "_score": 1,
        "_source": {
          "id": "3",
          "name": "我们都是好孩子",
          "age": 30,
          "createDate": "2021-03-01 09:03:04"
        }
      }
    ]
  }
}

作者:杜云明 欢迎转载,与人分享是进步的源泉!

如果觉得本文对您有所帮助,您可以点赞收藏。

内容有不对的地方欢迎批评指正。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕菲烟云

坚持原创,希望大家多多交流

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值