`

elasticsearch的Mapping定义

 
阅读更多
Mapping,就是对索引库中索引的字段名及其数据类型进行定义,类似于关系数据库中表建立时要定义字段名及其数据类型那样,不过es的mapping比数据库灵活很多,它可以动态添加字段。一般不需要要指定mapping都可以,因为es会自动根据数据格式定义它的类型,如果你需要对某些字段添加特殊属性(如:定义使用其它分词器、是否分词、是否存储等),就必须手动添加mapping。有两种添加mapping的方法,一种是定义在配置文件中,一种是运行时手动提交mapping,两种选一种就行了。

注意:
1、相当于数据库的表结构的定义,elasticsearch的mapping 也很重要。直接关系到性能及搜索结果的准确性。
2、elasticsearch的field一旦定义后就无法修改,增删改字段的话。从头定义表和建索引

先介绍在配置文件中定义mapping,你可以把[mapping名].json文件放到config/mappings/[索引名]目录下,这个目录要自己创建,一个mapping和一个索引对应,你也可以定义一个默认的mapping,把自己定义的default-mapping.json放到config目录下就行。json格式如下:
{
   "mappings":{
      "properties":{
         "title":{
            "type":"string",
            "store":"yes"
         },
         "description":{
            "type":"string",
            "index":"not_analyzed"
         },
         "price":{
            "type":"double"
         },
         "onSale":{
            "type":"boolean"
         },
         "type":{
            "type":"integer"
         },
         "createDate":{
            "type":"date"
         }
      }
   }
}


接下来介绍通过请求添加mapping,下面为一个添加productIndex索引库的mapping的json格式请求。其中productIndex为索引类型,properties下面的为索引里面的字段,type为数据类型,store为是否存储,”index”:”not_analyzed”为不对该字段进行分词。
{
   "productIndex":{
      "properties":{
         "title":{
            "type":"string",
            "store":"yes"
         },
         "description":{
            "type":"string",
            "index":"not_analyzed"
         },
         "price":{
            "type":"double"
         },
         "onSale":{
            "type":"boolean"
         },
         "type":{
            "type":"integer"
         },
         "createDate":{
            "type":"date"
         }
      }
   }
}

用java api调用的代码如下:
先创建空索引库
client.admin().indices().prepareCreate("productIndex").execute().actionGet();


put mapping
XContentBuilder mapping = jsonBuilder()
       .startObject()
         .startObject("productIndex")
         .startObject("properties")       
           .startObject("title").field("type", "string").field("store", "yes").endObject()  
           .startObject("description").field("type", "string").field("index", "not_analyzed").endObject()
           .startObject("price").field("type", "double").endObject()
           .startObject("onSale").field("type", "boolean").endObject()
           .startObject("type").field("type", "integer").endObject()
           .startObject("createDate").field("type", "date").endObject()               
         .endObject()
        .endObject()
      .endObject();
 PutMappingRequest mappingRequest = Requests.putMappingRequest("productIndex").type("productIndex").source(mapping);
 client.admin().indices().putMapping(mappingRequest).actionGet();


讲完mapping的定义后,给个例子:

为了说明mapping的定义,我这里定义了一个简单的模型,就ID,type,和catIds 3个属性,重在说明如何使用java api来定义mapping,具体各field应该如何定义,这里不做讨论。

public class TestModel implements Serializable {
	private static final long serialVersionUID = 3174577828007649745L;
	//主ID
	private long id;
	//类型,为types之一
	private String type;
	/**
	 * 这里是一个列表
	 */
	private List<Integer> catIds;
	
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public List<Integer> getCatIds() {
		return catIds;
	}
	public void setCatIds(List<Integer> catIds) {
		this.catIds = catIds;
	}
}

我们假设id就存储为long类型,type存储为字符串类型,catIds为一个列表,其实际类型为integer类型。定义的mapping如下:

/**
	 * mapping 一旦定义,之后就不能修改。
	 * @return
	 * @throws Exception
	 */
	private static XContentBuilder getMapping() throws Exception{
		XContentBuilder mapping = jsonBuilder()  
			       .startObject()  
			         .startObject("test")  
			         .startObject("properties")         
			           .startObject("id")
			           		.field("type", "long")
			           		.field("store", "yes")
			           	.endObject()    
			           	
			           .startObject("type")
			           		.field("type", "string")
			           		.field("index", "not_analyzed")
			           	.endObject()  
			           	
			           .startObject("catIds")
			           		.field("type", "integer")
			           .endObject()  
			         .endObject()  
			        .endObject()  
			      .endObject();  
		return mapping;
	}

注意:elasticsearch的field一旦定义后就无法修改,你想增加一个store属性,都不行。

下面就是调用JAVA API了,注意,在定义mapping之前,还需要先创建一个index库。这里,我index库和mapping 写到一个方法里面了。

               Client client = ESUtils.getClient();
//首先创建索引库
CreateIndexResponse  indexresponse = client.admin().indices()
//这个索引库的名称还必须不包含大写字母
.prepareCreate("testindex").execute().actionGet();
System.out.println(indexresponse.acknowledged());

//如果是在两台机器上,下面直接putMapping可能会报异常
PutMappingRequestBuilder builder = client.admin().indices().preparePutMapping("testindex");
//testType就像当于数据的table
builder.setType("testType");
XContentBuilder mapping = getMapping();
builder.setSource(mapping);
PutMappingResponse  response = builder.execute().actionGet();
System.out.println(response.acknowledged());
其中,这个代码在我本机出现一点问题,当我创建完index后,直接创建mapping 的时候,报index missing。我把两个es Node停掉一个就没有问题了。可见,ES将create index和putMapping放到了两个不同的es Node下面,导致了上面那个异常。

好了,有时为了测试,可能需要删除某个索引,代码如下:
Client client = ESUtils.getClient();
client.admin().indices()
//这个索引库的名称还必须不包含大写字母
.prepareDelete("testindex").execute().actionGet();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics