0. 索引分词概念

index:默认true,设置为false的话,那么这个字段就不会被索引
引用官文:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-index.html

index
The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.

1. 创建索引的同时创建mappings

  1. PUT /index_str
  2. {
  3. "mappings": {
  4. "properties": {
  5. "realname": {
  6. "type": "text",
  7. "index": true
  8. },
  9. "username": {
  10. "type": "keyword",
  11. "index": false
  12. }
  13. }
  14. }
  15. }

2.查看分词效果

  1. GET /index_mapping/_analyze
  2. {
  3. "field": "realname",
  4. "text": "imooc is good"
  5. }

3. 尝试修改

  1. POST /index_str/_mapping
  2. {
  3. "properties": {
  4. "name": {
  5. "type": "long"
  6. }
  7. }
  8. }

4. 为已存在的索引创建或创建mappings

  1. POST /index_str/_mapping
  2. {
  3. "properties": {
  4. "id": {
  5. "type": "long"
  6. },
  7. "age": {
  8. "type": "integer"
  9. },
  10. "nickname": {
  11. "type": "keyword"
  12. },
  13. "money1": {
  14. "type": "float"
  15. },
  16. "money2": {
  17. "type": "double"
  18. },
  19. "sex": {
  20. "type": "byte"
  21. },
  22. "score": {
  23. "type": "short"
  24. },
  25. "is_teenager": {
  26. "type": "boolean"
  27. },
  28. "birthday": {
  29. "type": "date"
  30. },
  31. "relationship": {
  32. "type": "object"
  33. }
  34. }
  35. }
  • 注:某个属性一旦被建立,就不能修改了,但是可以新增额外属性

    主要数据类型

  • text, keyword, string

  • long, integer, short, byte
  • double, float
  • boolean
  • date
  • object
  • 数组不能混,类型一致

    字符串

  • text:文字类需要被分词被倒排索引的内容,比如商品名称,商品详情,商品介绍,使用text。

  • keyword:不会被分词,不会被倒排索引,直接匹配搜索,比如订单状态,用户qq,微信号,手机号等,这些精确匹配,无需分词。