Elasticsearch:Mapping-映射

一、创建索引 自动生成索引字段数据类型即自动映射

创建之前,先删除索引防止重复创建
删除索引:

bash 复制代码
DELETE product_mapping

创建索引 product_mapping并且赋值

bash 复制代码
PUT /product_mapping/_doc/1
{
 "name": "xiaomi phone",
 "desc": "shouji zhong de zhandouji",
 "count": 123456,
 "price": 123.123,
 "date": "2020-05-20",
 "isdel": false,
 "tags": [
 "xingjiabi",
 "fashao",
 "buka"
 ]
}

执行结束:索引创建成功。

bash 复制代码
{
  "_index" : "product_mapping",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

查询索引

bash 复制代码
GET product_mapping/_search

执行结果:

bash 复制代码
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "product_mapping",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "xiaomi phone",
          "desc" : "shouji zhong de zhandouji",
          "count" : 123456,
          "price" : 123.123,
          "date" : "2020-05-20",
          "isdel" : false,
          "tags" : [
            "xingjiabi",
            "fashao",
            "buka"
          ]
        }
      }
    ]
  }
}

查询索引自动生成的映射

bash 复制代码
GET product_mapping/_mapping

结果如下:

bash 复制代码
{
  "product_mapping" : {
    "mappings" : {
      "properties" : {
        "count" : {
          "type" : "long"
        },
        "date" : {
          "type" : "date"
        },
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isdel" : {
          "type" : "boolean"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "price" : {
          "type" : "float"
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

总结:

根据put创建索引赋值的时候每个字段的数据,动态的生成了字段的属性,可以类比mysql的表结构的字段属性,这里重点介绍一下text类型,即文本类型,在创建索引的时候,如果是text类型的,那么会对这个字段进行索引,生成倒排索引进行存储,后续查询的时候,如果查询方式也支持对搜索的内容进行索引分词,那么就会把搜索条件分词后的词项和生成索引的时候索引的列的词项进行匹配。

bash 复制代码
"fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }

这个keyword,不会分词,如果text需要精准匹配,可以用该字段的keyword.

1."name": "xiaomi phone",在创建索引的时候,这个会被分词为xiaomi 和 phone两个词项,下面的查询方式

搜索内容无论是xiaomi 或者 phone 还是两个顺序颠倒,都能匹配到,因为会对搜索条件进行分词为xiaomi 或者 phone,匹配索引分词后的词根会匹配到。

bash 复制代码
GET product_mapping/_search
{
  "query": {
    "match": {
      "name": "phone xiaomi "
    }
  }
}

name.keyword 就是查询索引中name分词前为xiaomi 的内容,因为name分词前只有xiaomi phone,所以这样查询不到

bash 复制代码
GET product_mapping/_search
{
  "query": {
    "match": {
      "name.keyword": " xiaomi "
    }
  }
}

下面这样的方式可以匹配的到

bash 复制代码
GET product_mapping/_search
{
  "query": {
    "match": {
      "name.keyword": "xiaomi phone"
    }
  }
}

二、创建索引 手动映射数据类型

手动创建索引和mapping映射

bash 复制代码
PUT /product
{
 "mappings": {
 "properties": {
 "date": {
 "type": "text"
 },
 "desc": {
 "type": "text",
 "analyzer": "english"
 },
 "name": {
 "type": "text",
 "index": "false"
 },
 "price": {
 "type": "long"
 },
 "tags": {
 "type": "text",
 "index": "true"
 },
 "parts": {
 "type": "object"
 },
 "partlist": {
 "type": "nested"
 }
 }
 }
}

name的属性"index": "false"表示创建索引的时候不进行分词。

bash 复制代码
GET /product/_search
{
  "query": {
    "match": {
      "name": "xiaomi"
    }
  }
}

执行会报错

bash 复制代码
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot search on field [name] since it is not indexed.",
        "index_uuid" : "wEUFSz6VQmaC6Ko9oE3cxQ",
        "index" : "product"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "product",
        "node" : "QaQYrOAFRr2kwyt6IuJU9Q",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot search on field [name] since it is not indexed.",
          "index_uuid" : "wEUFSz6VQmaC6Ko9oE3cxQ",
          "index" : "product",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Cannot search on field [name] since it is not indexed."
          }
        }
      }
    ]
  },
  "status" : 400
}
相关推荐
逸模4 小时前
告别熬夜手工整理台账,逸模智能归集实现项目数据自动化存档
大数据·运维·人工智能·笔记·其他·信息可视化·自动化
audyxiao0016 小时前
ICLR 2026论文分享 | WorldGym:用世界模型打造机器人策略评估新范式
大数据·人工智能·大模型·智能体·世界模型
Rubin智造社6 小时前
Anthropic安全白皮书2|三级成熟度模型:你的AI智能体该配哪级安全?
大数据·安全·沙箱隔离·零信任成熟度模型·三级安全框架·jit权限·不可变审计
ACP广源盛139246256737 小时前
GSV2221 显示转换芯片@ACP#赋能 RTX Spark 端侧 AI 设备,构建多屏全模态视觉交互新生态
大数据·人工智能·嵌入式硬件·gpt·spark·电脑·音视频
字节跳动开源7 小时前
你的 Agent 每次都“失忆”?这个工具彻底治好了我的前端开发焦虑
大数据·开源·agent
APItesterCris8 小时前
实战教程:借助 Open Claw + 淘宝商品 API,低成本实现电商自动化监控与智能选品
大数据·运维·自动化
团象科技9 小时前
从一线运营场景观察 海外云 独立站的跨境效能释放实践路径
大数据·人工智能
宸津-代码粉碎机9 小时前
Spring AI企业级实战|从RAG优化到Agent多工具调度
java·大数据·人工智能·后端·python·spring
INFINI Labs9 小时前
Elasticsearch 6/7/8 到 Easysearch 2.x 迁移指南
大数据·elasticsearch·mybatis·向量·snapshot
小柒儿3369 小时前
汪进进:深水区里以质立身,做长期价值的践行者
大数据·人工智能