zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

elasticsearch中常用Mapping参数设置

elasticsearch 常用 Mapping 参数设置
2023-09-11 14:15:14 时间

index:控制当前字段是否被索引,默认为true。如果设置为false,该字段不可被搜索。

创建新的索引并设置mapping信息

# 删除已经存在的索引(删除谨慎操作)
DELETE user

# 创建新的索引并设置mapping信息
PUT user
{
  "mappings": {
    "properties": {
      "address": {
        "type": "text",
        "index": false
      },
      "age": {
        "type": "long"
      },
      "name": {
        "type": "text"
      }
    }
  }
}

运行结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "user"
}

在新索引中添加一行数据

# 在新索引中添加一行数据
PUT user/_doc/1
{
  "name": "张三",
  "age": 26,
  "address": "北京白云山公园"
}

运行结果:

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

查询索引信息

# 查询索引信息
GET user

运行结果:

{
  "user" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text",
          "index" : false
        },
        "age" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "user",
        "creation_date" : "1667349588021",
        "number_of_replicas" : "1",
        "uuid" : "VupGOi5jTiWFWN4i5xNKjA",
        "version" : {
          "created" : "7170699"
        }
      }
    }
  }
}

查询某条数据

# 查询某条数据
GET user/_search
{
  "query": {
    "match": {
      "address": "北京"
    }
  }
}

运行结果:  【"address"字段没有创建索引,是不能根据此字段进行查询的】

{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot search on field [address] since it is not indexed.",
        "index_uuid" : "VupGOi5jTiWFWN4i5xNKjA",
        "index" : "user"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "user",
        "node" : "9xCKv5RGRNecuoPworyaUg",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot search on field [address] since it is not indexed.",
          "index_uuid" : "VupGOi5jTiWFWN4i5xNKjA",
          "index" : "user",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Cannot search on field [address] since it is not indexed."
          }
        }
      }
    ]
  },
  "status" : 400
}

有四种不同基本的index options配置,控制倒排索引记录的内容:

  • docs :记录doc id
  • freqs:记录doc id和term frequencies (词频)
  • positions:记录doc id / term frequencies / term position
  • offsets: doc id / term frequencies / term posistion / character offects

text类型默认记录postions,其他默认为docs。记录内容越多,占用存储空间越大。

创建新的索引mapping,index_options设置为"offsets"

# 删除已经存在的索引(删除谨慎操作)
DELETE user

# 创建新的索引并设置mapping信息
PUT user
{
  "mappings": {
    "properties": {
      "address": {
        "type": "text",
        "index_options": "offsets"
      },
      "age": {
        "type": "long"
      },
      "name": {
        "type": "text"
      }
    }
  }
}

运行结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "user"
}

查看索引

# 查看索引
GET user/_mapping

运行结果:

{
  "user" : {
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text",
          "index_options" : "offsets"
        },
        "age" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

在新索引中添加一行数据

# 在新索引中添加一行数据
PUT user/_doc/1
{
  "name": "张三",
  "age": 26,
  "address": "北京白云山公园"
}

运行结果:

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

查询索引信息:

# 查询索引信息
GET user

运行结果:

{
  "user" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "text",
          "index_options" : "offsets"
        },
        "age" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "user",
        "creation_date" : "1667436480511",
        "number_of_replicas" : "1",
        "uuid" : "KLUzbYpGRAm_kPPPRmYfHw",
        "version" : {
          "created" : "7170699"
        }
      }
    }
  }
}

查询某条数据

# 查询某条数据
GET user/_search
{
  "query": {
    "match": {
      "address": "北京"
    }
  }
}

运行结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "张三",
          "age" : 26,
          "address" : "北京白云山公园"
        }
      }
    ]
  }
}

注意:本人分别将index_options更换为其它3中,发现查询结果没有区别!想想应该是测试数据只有1条的原因,有待验证!

null_value:需要对Null值进行搜索,只有keyword类型支持设计Null_Value

创建新的索引mapping,并将null_value设置为NULL

# 删除已经存在的索引(删除谨慎操作)
DELETE user

# 创建新的索引并设置mapping信息
PUT user
{
  "mappings": {
    "properties": {
      "address": {
        "type": "keyword",
        "null_value": "NULL"
      },
      "age": {
        "type": "long"
      },
      "name": {
        "type": "text"
      }
    }
  }
}

运行结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "user"
}

查看索引

# 查看索引
GET user/_mapping

运行结果:

{
  "user" : {
    "mappings" : {
      "properties" : {
        "address" : {
          "type" : "keyword",
          "null_value" : "NULL"
        },
        "age" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

在新索引中添加一行数据

# 在新索引中添加一行数据
PUT user/_doc/1
{
  "name": "张三",
  "age": 26,
  "address": null
}

运行结果:

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

查看索引并查询某条NULL数据

# 查询索引信息
GET user

# 查询某条数据
GET user/_search
{
  "query": {
    "match": {
      "address": "NULL"
    }
  }
}

运行结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "张三",
          "age" : 26,
          "address" : null
        }
      }
    ]
  }
}