溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Elasticsearch常用操作解析

發(fā)布時間:2020-06-08 08:57:19 來源:網絡 閱讀:240 作者:Stitch_x 欄目:大數據

創(chuàng)建Maven管理的Java項目

在pom.xml中添加依賴:

<es.version>6.1.1</es.version>

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>${es.version}</version>
</dependency>

然后創(chuàng)建一個單元測試類ESApp:

private TransportClient client;

    @Before
    public void setUp() throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", "mycluster")
                .put("client.transport.sniff", "true")//增加自動嗅探配置
                .build();

        client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("10.8.24.94"), 9300));

        System.out.println(client.toString());
    }

運行后報錯

java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.3</version>
</dependency>

運行后成功拿到ES的client:

Elasticsearch常用操作解析

創(chuàng)建一個Index

@Test
    public void createIndex() {
        client.admin().indices().prepareCreate(INDEX).get();
        System.out.println("創(chuàng)建Index成功");
    }

刪除一個Index

@Test
    public void deleteIndex() {
        client.admin().indices().prepareDelete(INDEX).get();
        System.out.println("刪除Index成功");
    }

放入數據的三種方式

//不推薦使用,太繁瑣拼json格式
 @Test
    public void createDoc() {
        String json = "{\"name\":\"若澤數據\"}";

        IndexResponse response = client.prepareIndex(INDEX, TYPE, "100")
                .setSource(json, XContentType.JSON)
                .get();
    }

    //推薦使用
    @Test
    public void test01() throws Exception {
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("name", "ruozedata");
        json.put("message", "trying out Elasticsearch");

        IndexResponse response = client.prepareIndex(INDEX, TYPE, "101").setSource(json).get();
        System.out.println(response.getVersion());
    }

//推薦使用
    @Test
    public void test02() throws Exception {

        XContentBuilder builder = jsonBuilder()
                .startObject()
                .field("user", "ruoze")
                .field("postDate", new Date())
                .field("message", "trying out Elasticsearch")
                .endObject();

        IndexResponse response = client.prepareIndex(INDEX, TYPE, "102").setSource(builder).get();
        System.out.println(response.getVersion());
    }

拿到一條數據

@Test
    public void getDoc() {
        GetResponse response = client.prepareGet(INDEX, TYPE, "100").get();
        System.out.println(response.getSourceAsString());
    }

拿到多條數據

@Test
    public void getDocsByIds() {

        MultiGetResponse responses = client.prepareMultiGet()
                .add(INDEX, TYPE,"100")
                .add(INDEX, TYPE, "101", "102", "1000")
                .get();

        for (MultiGetItemResponse response : responses) {
            GetResponse res = response.getResponse();
            if (res.isExists()) {
                System.out.println(res);
            } else {
                System.out.println("沒有這條數據");
            }

        }
    }
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI