elasticsearch 建立索引以及设置相关 field属性

本文介绍如何使用Java API在Elasticsearch中创建索引,并设置不同字段的存储及索引方式,包括全文检索和非全文检索字段的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值