当前位置: 首页 > elasticsearch, 搜索 > 正文

elasticsearch 获取mapping结果

关键字:
1 星2 星3 星4 星5 星 (暂无评分)
Loading ... Loading ...
baidu_share

elasticsearch 创建一个mapping方法:

public XContentBuilder createMapping(String indexType){
		XContentBuilder mapping = null;
		try {
			mapping = jsonBuilder()
					.startObject()
						// 索引库名(类似数据库中的表)
						.startObject(indexType)
 
							// 索引里面的字段
							.startObject("properties")
								// 新闻ID
								.startObject("id")
									.field("type", "long")
									.field("store", "yes")
									.field("index", "not_analyzed")
									.field("include_in_all", "false")
								.endObject()
 
								// 新闻标题
								.startObject("title")
									.field("type", "string")
									.field("store", "yes")
									.field("term_vector","with_positions_offsets")
									.field("indexAnalyzer", "ik")
									.field("searchAnalyzer", "ik")
									.field("include_in_all", "false")
									.field("boost", 4.0) // 打分(默认1.0)
								.endObject()
 
							.endObject()
						.endObject()
					.endObject();
		} catch (IOException e) {
			logger.error("createMapping error:",e);
		}
 
		return mapping;
	}
	public void createIndex(String indexName, String indexType){
 
 
	    Client esClient = nodeBuilder()
                //.local(true)
                .settings(settingsBuilder()
                        .put("cluster.name", "itest-cluster-" + NetworkUtils.getLocalAddress())
                        //.put("gateway.type", "none")
                ).node().client();
 
	    // 创建Mapping
	    XContentBuilder mapping = this.createMapping(indexType);
		// 创建一个空索引
		esClient.admin().indices().prepareCreate(indexName).execute().actionGet();
 
		PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(indexType).source(mapping);
		esClient.admin().indices().putMapping(mappingRequest).actionGet();
 
	}

创建索引后,查看mapping结果是否正确:

//创建一个itest索引,索引类型为news。
ImmutableOpenMap<String, MappingMetaData> mappings =  node.client().admin().cluster().prepareState().execute().actionGet().getState().getMetaData().getIndices().get("itest").getMappings();
System.out.println(mappings.get("news").source());

本文固定链接: http://www.chepoo.com/elasticsearch-get-mapping-results.html | IT技术精华网

elasticsearch 获取mapping结果:等您坐沙发呢!

发表评论