Elasticsearch教程(五) elasticsearch Mapping的创建

一、Mapping介绍

 Elasticsearch  中, Mapping  是什么?

mapping   Elasticsearch  中的作用就是约束。

1.数据类型声明

它类似于静态语言中的数据类型声明,比如声明一个字段为String, 以后这个变量都只能存储String类型的数据。同样的, 一个number类型的 mapping  字段只能存储number类型的数据。

2.Mapping它定义了 Type 的属性。

  1. "_ttl": {"enabled": false

表示 ttl关闭,其实ttl默认就是关闭。

3.指定分词器。

 
 
  1. "id": {
  2. "index": "not_analyzed",
  3. "type": "string"
  4. }

指定字段 id不分词,并且类型为 string

4.…………

二、创建Mapping


1.下面介绍一下HTTP的创建方式。我一般用Java 创建方式。

  1. PUT http://123.123.123.123:9200/index/type/
  2. {
  3. "settings": {
  4. //设置10个分片,理解为类似数据库中的表分区中一个个分区的概念,不知道是否妥当
  5. "number_of_shards": 10
  6. },
  7. "mappings": {
  8. "trades": {
  9. "_id": {
  10. "path": "id"
  11. },
  12. "properties": {
  13. "id": {
  14. "type": "integer",
  15. //id:自增数字
  16. //要求:查询
  17. "store" : true
  18. },
  19. "name": { //名称
  20. "type": "string"
  21. },
  22. "brand": { //品牌: PG,P&G,宝洁集团,宝洁股份,联想集团,联想电脑等
  23. "type": "string"
  24. },
  25. "orderNo": { //订单号 :如ATTS000928732
  26. "type": "string",
  27. "index": "not_analyzed"
  28. },
  29. "description": {
  30. //描述: 2015款玫瑰香型强生婴儿沐浴露,550ml,包邮
  31. "type": "string"
  32. "sort": true
  33. },
  34. "date": {
  35. "type": "date"
  36. },
  37. "city": {
  38. "type": "string"
  39. },
  40. "qty": {// index分词无效
  41. "type": "float"
  42. },
  43. "price": {
  44. //价格: float index无效
  45. "type": "float"
  46. }
  47. }
  48. }
  49. }
  50. }

上面是从其他地方抄过来的。因为我不用这种方式。

2.Java方式创建。

构建 Mapping  

 
 
  1. package com.sojson.core.elasticsearch.mapping;
  2. import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
  3. import java.io.IOException;
  4. import org.elasticsearch.common.xcontent.XContentBuilder;
  5. public class ZhidaoMapping {
  6. public static XContentBuilder getMapping(){
  7. XContentBuilder mapping = null;
  8. try {
  9. mapping = jsonBuilder()
  10. .startObject()
  11. //开启倒计时功能
  12. .startObject("_ttl")
  13. .field("enabled",false)
  14. .endObject()
  15. .startObject("properties")
  16. .startObject("title")
  17. .field("type","string")
  18. .endObject()
  19. .startObject("question")
  20. .field("type","string")
  21. .field("index","not_analyzed")
  22. .endObject()
  23. .startObject("answer")
  24. .field("type","string")
  25. .field("index","not_analyzed")
  26. .endObject()
  27. .startObject("category")
  28. .field("type","string")
  29. .field("index","not_analyzed")
  30. .endObject()
  31. .startObject("author")
  32. .field("type","string")
  33. .field("index","not_analyzed")
  34. .endObject()
  35. .startObject("date")
  36. .field("type","string")
  37. .field("index","not_analyzed")
  38. .endObject()
  39. .startObject("answer_author")
  40. .field("type","string")
  41. .field("index","not_analyzed")
  42. .endObject()
  43. .startObject("answer_date")
  44. .field("type","string")
  45. .field("index","not_analyzed")
  46. .endObject()
  47. .startObject("description")
  48. .field("type","string")
  49. .field("index","not_analyzed")
  50. .endObject()
  51. .startObject("keywords")
  52. .field("type","string")
  53. .field("index","not_analyzed")
  54. .endObject()
  55. .startObject("read_count")
  56. .field("type","integer")
  57. .field("index","not_analyzed")
  58. .endObject()
  59. //关联数据
  60. .startObject("list").field("type","object").endObject()
  61. .endObject()
  62. .endObject();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. return mapping;
  67. }
  68. }

创建 Mapping  

  1. public static void createBangMapping(){
  2. PutMappingRequest mapping = Requests.putMappingRequest(INDEX).type(TYPE).source(ZhidaoMapping.getMapping());
  3. ESTools.client.admin().indices().putMapping(mapping).actionGet();
  4. }

创建的时候,需要 index已经创建才行,要不然会报错。

 
 
  1. //构建一个Index(索引) CreateIndexRequest request = new CreateIndexRequest(INDEX);
  2. ESTools.client.admin().indices().create(request);

创建完毕在 Head  插件里查看或者Get请求。

  1. http://123.123.123.123:9200/index/type/_mapping

得到的结果:

 
 
  1. {
  2. "zhidao_index": {
  3. "mappings": {
  4. "zhidao_type": {
  5. "_ttl": {
  6. "enabled": false
  7. },
  8. "properties": {
  9. "answer": {
  10. "type": "string",
  11. "index": "not_analyzed"
  12. },
  13. "answerAuthor": {
  14. "type": "string"
  15. },
  16. "answerDate": {
  17. "type": "date",
  18. "format": "strict_date_optional_time||epoch_millis"//这里出现了复合类型
  19. },
  20. "answer_author": {
  21. "type": "string",
  22. "index": "not_analyzed"
  23. },
  24. "answer_date": {
  25. "type": "string",
  26. "index": "not_analyzed"
  27. },
  28. "author": {
  29. "type": "string",
  30. "index": "not_analyzed"
  31. },
  32. "category": {
  33. "type": "string",
  34. "index": "not_analyzed"
  35. },
  36. "date": {
  37. "type": "string",
  38. "index": "not_analyzed"
  39. },
  40. "description": {
  41. "type": "string",
  42. "index": "not_analyzed"
  43. },
  44. "id": {
  45. "type": "string",
  46. "index": "not_analyzed"
  47. },
  48. "keywords": {
  49. "type": "string",
  50. "index": "not_analyzed"
  51. },
  52. "list": {
  53. "type": "object"
  54. },
  55. "question": {
  56. "type": "string",
  57. "index": "not_analyzed"
  58. },
  59. "readCount": {
  60. "type": "long"
  61. },
  62. "read_count": {
  63. "type": "integer"
  64. },
  65. "title": {
  66. "type": "string"
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }

Head插件查看

其实 Mapping  ,你接触 Elasticsearch  久一点也就那么回事。我们虽然知道 Elasticsearch  有根据数据识别创建 Mapping  ,但是最好是创建,并且指定分词与否。这样高效一点。

猜你喜欢

转载自blog.csdn.net/superviser3000/article/details/80856547
今日推荐