elasticsearch 建立索引以及设置相关 field属性
来源:互联网 发布:网络成瘾的治疗 编辑:程序博客网 时间:2024/05/27 19:26
package com.weibo.dip.kte;import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.client.Requests;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentBuilder;import org.elasticsearch.common.xcontent.XContentFactory;import java.io.IOException;import java.net.InetAddress;import java.net.UnknownHostException;import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;/** * 根据需要创建索引,将不需要全文检索的字段的属性设置为 not_analyzed,还可以设置其他属性 * Created by fpschina on 16/2/29. */public class CreateIndex { private TransportClient client = null; public CreateIndex() throws UnknownHostException { Settings settings = Settings.settingsBuilder() .put("cluster.name", "dip-application") .build(); client = TransportClient.builder().settings(settings).build(); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.13.56.52"), 9300)); } public static void myPrint() throws IOException { XContentBuilder builder = XContentFactory .jsonBuilder() .startObject() .startObject("sina") .startObject("properties") .startObject("article_title") .field("type", "string") .field("store", "yes") .field("index", "not_analyzed") .endObject() .startObject("article_content") .field("type", "string") .field("store", "no") .field("index", "analyzed") .endObject() .startObject("article_url") .field("type", "string") .field("store", "yes") .field("index", "not_analyzed") .endObject() .endObject() .endObject() .endObject(); System.out.println(builder.string()); } public void createIndex() throws IOException { XContentBuilder builder = XContentFactory .jsonBuilder() .startObject() .startObject("sina") .startObject("properties") .startObject("article_title") .field("type", "string") .field("store", "yes") .field("index", "not_analyzed") .endObject() .startObject("article_content") .field("type", "string") .field("store", "no") .field("index", "analyzed") .endObject() .startObject("article_url") .field("type", "string") .field("store", "yes") .field("index", "not_analyzed") .endObject() .endObject() .endObject() .endObject(); System.out.println(builder.string()); client.admin().indices().prepareCreate("pages").execute().actionGet(); PutMappingRequest putMappingRequest = Requests.putMappingRequest("pages").type("sina").source(builder); client.admin().indices().putMapping(putMappingRequest).actionGet(); client.close(); } public void postData() throws IOException { IndexResponse response = client.prepareIndex("pages", "sina", null) .setSource(jsonBuilder() .startObject() .field("article_title", "myTitle") .field("article_content", "myContent") .field("article_url", "www.sina.com") .endObject() ) .execute() .actionGet(); } public static void main(String[] args) { try { CreateIndex indexTest = new CreateIndex(); indexTest.createIndex(); indexTest.postData(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
ES版本2.2.0
0 0
- elasticsearch 建立索引以及设置相关 field属性
- Elasticsearch Java API(九)--删除文档的field以及field的属性
- ElasticSearch 索引相关操作
- elasticsearch-设置索引
- ElasticSearch建立索引报错
- elasticsearch基础知识以及创建索引
- ElasticSearch中的field data(正排索引)
- Elasticsearch修改Mpping和在索引上添加field
- flume建立ElasticSearch索引时间的问题
- elasticsearch建立索引操作的API
- elasticsearch建立索引操作的API
- Lucence建立索引时,Field建立索引时分词存储索引NORMS问题
- 建立ORALCE空间索引相关
- 网页Cookie设置作用以及相关Cookies属性分析
- ElasticSearch索引的相关操作---javaEE--SpringDataElasticSearch
- 数据库建立索引以及索引失效问题
- elasticsearch 自定义索引 _all 设置规则
- elasticsearch-索引分片和副本设置
- sql case when用法
- Object.prototype.toString跨框架判断类型
- Git查看、删除、重命名远程分支和tag
- servlet之response
- 如何使用Android studio打开eclipse项目
- elasticsearch 建立索引以及设置相关 field属性
- 联想Yoga13/Ubuntu14.04:Wifi连接问题的解决方法
- 母牛故事 2018
- mysql 报错 query is too large (1041 > 1024) 处理
- J2EE中容器:WEB容器、EJB容器
- Java 异常处理
- php+mysql在mac上的基本配置
- MavenEclipse
- java并发编程