• elasticsearch JAVA API小例子


    package taotao.maozi.maven_es;

    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.search.SearchType;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.Client;
    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 org.elasticsearch.index.get.GetField;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.junit.Before;
    import org.junit.Test;

    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.Date;
    import java.util.Map;
    import java.util.concurrent.ExecutionException;

    /**
    * Created by root on 2016/3/26 0026.
    */
    public class App {
    Client client = null;
    /**
    * 预加载
    * @throws UnknownHostException
    */
    @Before
    public void init() throws Exception {

    Settings settings = Settings.settingsBuilder().put("cluster.name", "my-application").build();
    client = TransportClient.builder().settings(settings).build()
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("www.master.com"), 9300))
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("www.slave1.com"), 9300))
    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("www.slave2.com"), 9300));

    }

    /**
    * 建立索引
    * @throws Exception
    */
    @Test
    public void crete() throws Exception {

    XContentBuilder builder = XContentFactory.jsonBuilder()
    .startObject()
    .field("user", "123456")
    .field("postDate", new Date())
    .field("message", "trying out Elasticsearch!!!!!!!!!!!!!!")
    .endObject();
    String json = builder.string();
    //建立索引
    IndexResponse response = client.prepareIndex("bjsxt", "emp", "1").setSource(json).execute().actionGet();

    System.out.println("建立索引:===" + response);
    }



    /**
    * 搜索1
    */
    @Test
    public void such() {

    //搜索api
    SearchResponse response = client.prepareSearch("bjsxt")
    .setTypes("emp")
    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
    .setQuery(QueryBuilders.termQuery("user", "123456")) // Query
    // .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
    .setFrom(0).setSize(1).setExplain(true)
    .execute()
    .actionGet();

    SearchHits hits = response.getHits();
    System.out.println(hits.getHits().length);
    SearchHit[] hits1 = hits.getHits();
    for(SearchHit hit :hits1){
    Map<String, Object> source = hit.getSource();
    for(Map.Entry<String,Object> filed :source.entrySet()){
    String key = filed.getKey();
    System.out.println("key===="+key+" value====="+filed.getValue().toString());
    }
    }


    }

    /**
    * 搜索002
    * <p>Title: such02</p>
    * <p>Description: </p>
    */
    @Test
    public void such02() {
    // 查询索引两种方式
    GetResponse response = client.prepareGet("bjsxt", "emp", "1").execute().actionGet();
    //GetResponse response = client.prepareGet("bjsxt", "emp", "1").get();
    Map<String, Object> source = response.getSource();
    for (Map.Entry<String, Object> filed : source.entrySet()) {
    String key = filed.getKey();
    System.out.println("key====" + key + " name=====" + filed.getValue().toString());
    }

    }

    /**
    * 修改
    * @throws IOException
    * @throws ExecutionException
    * @throws InterruptedException
    */
    @Test
    public void update() throws Exception {

    XContentBuilder builder = XContentFactory.jsonBuilder()
    .startObject()
    .field("user", "123456")
    .field("postDate", new Date())
    .field("message", "trying out update8833388668")
    .endObject();
    String json = builder.string();

    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("bjsxt");
    updateRequest.type("emp");
    updateRequest.id("1");
    updateRequest.doc(builder);
    UpdateResponse updateResponse = client.update(updateRequest).get();
    // System.out.println("===="+updateResponse.+ "=====");

    /* Map<String, Object> source = updateResponse.getGetResult().getSource();
    for (Map.Entry<String, Object> filed : source.entrySet()) {
    String key = filed.getKey();
    System.out.println("key====" + key + " name=====" + filed.getValue().toString());
    }*/
    }


    /**
    * 删除
    */
    @Test
    public void del() {
    // 删除索引
    DeleteResponse response = client.prepareDelete("bjsxt", "emp", "1").get();
    System.out.println(response);
    }
    }

    =======================================

    maven 依赖

    maven 会自动解决依赖关系

    ------------------------------------------------------------

    <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.2.0</version>
    </dependency>

    --------------------------------------------------------------

  • 相关阅读:
    阿里云安全
    阿里云存储
    阿里云网络和CDN
    一文搞懂用户登录验证流程(附图)
    mybatis注解之@Mapper和@MapperScan的使用
    百度一面:谈谈 @Transactional 的原理和坑
    IDEA断点调试技巧,多张动图包教包会
    一文弄懂责任链设计模式
    美团一面:@Transactional 原理和常见的坑?
    RocketMQ消息丢失场景及解决办法
  • 原文地址:https://www.cnblogs.com/dcxmaozi/p/6664409.html
Copyright © 2020-2023  润新知