検索エンジンelasticsearch(四)--javaを使ってデータ操作を行います。

12306 ワード

一、概要
前の記事では、httpを使ってelasticsearchをデータ操作することを紹介しました。ここではjavaでelasticsearchを使用しています。
のJavaクライアントでデータ操作を行います。 ここで使っているesバージョンは6.2.2です。
二、ステップ
1、追加maven依存

    org.elasticsearch
    elasticsearch
    6.2.2


    org.elasticsearch.client
    transport
    6.2.2
2、javaの中でesデータは操作して、ここは直接コードをつけて、詳しくは注釈を見てください。
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.Map;

public class ElasticSearchPaperMain {

    public static void main(String[] args) throws Exception {
        //  cluster.name
        Settings settings = Settings.builder().put("cluster.name", "my-application")
                .put("client.transport.sniff", true)
                .build();
        //     ,  java     9300
        Client client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.107"), 9300));


        //  
        IndexResponse response = client.prepareIndex("stu", "doc","1")
                .setSource(
                        XContentFactory.jsonBuilder()
                                .startObject()
                                .field("stu_id", "001")
                                .field("stu_name", "apple1")
                                .endObject()
                ).get();
        System.out.println(response.toString());

        //  
        JSONObject obj = new JSONObject();
        obj.put("stu_name", "banana");
        UpdateResponse updateResponse = client.prepareUpdate("stu", "doc", "1")
                .setDoc(obj.toJSONString(), XContentType.JSON).get();
        System.out.println(updateResponse);


        //  
        DeleteResponse deleteResponse = client.prepareDelete("stu","doc","1").execute().actionGet();
        System.out.println(deleteResponse);

        //    
        GetResponse getResponse = client.prepareGet("stu", "doc","1").execute().actionGet();
        System.out.println(getResponse);

        //    
        QueryBuilder queryBuilder = QueryBuilders.disMaxQuery()
                .add(QueryBuilders.termQuery("stu_name", "apple1"));
        SearchResponse searchResponse = client.prepareSearch("stu").setQuery(queryBuilder).execute().actionGet();

        //    
        queryBuilder = QueryBuilders.wildcardQuery("stu_name", "apple*");
        searchResponse = client.prepareSearch("stu").setQuery(queryBuilder).execute().actionGet();
        System.out.println(searchResponse);

        //    
        searchResponse = client.prepareSearch("stu").execute().actionGet();
        System.out.println(searchResponse);
        
        //    
        searchResponse = client.prepareSearch("stu").addSort("_score", SortOrder.DESC).setFrom(2).setSize(2).execute().actionGet();
        System.out.println(searchResponse);
        
        Arrays.stream(searchResponse.getHits().getHits()).forEach(t->{
            Map, Object> m = t.getSourceAsMap();
            String stuId = (String) m.get("stu_id");
            String stuName = (String) m.get("stu_name");
            System.out.println(stuId+"   "+stuName);
        });
    }
}