Java教程

elasticsearch java api

本文主要是介绍elasticsearch java api,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.it.es;

import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequestBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import javax.xml.bind.SchemaOutputResolver;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * @author zhuxingyu
 *
 */
public class UpsertCarInfoApp {


    private static TransportClient client;

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //client集群自动探查client.transport.sniff使用上述的settings配置,将client.transport.sniff设置为true即可打开集群节点自动探查功能
        Settings setting = Settings.builder().put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true).build();     
        client = new PreBuiltTransportClient(setting)
                .addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        //  upsert();
        // multiGet();
        //bulk();
        //scroll();
        // template();
        // query();
        geoPoint();
        client.close();

    }

    /**
     *  区域定位查询
     */
    private static void geoPoint() {
        /**
         * 1、引入依赖
         * <dependency>
         *     <groupId>org.locationtech.spatial4j</groupId>
         *     <artifactId>spatial4j</artifactId>
         *     <version>0.6</version>
         * </dependency>
         *
         * <dependency>
         *     <groupId>com.vividsolutions</groupId>
         *     <artifactId>jts</artifactId>
         *     <version>1.13</version>
         *     <exclusions>
         *         <exclusion>
         *             <groupId>xerces</groupId>
         *             <artifactId>xercesImpl</artifactId>
         *         </exclusion>
         *     </exclusions>
         * </dependency>
         * 2、设置mapping
         * POST /car_shop/_mapping/shops
         * {
         *   "properties": {
         *       "pin": {
         *           "properties": {
         *               "location": {
         *                   "type": "geo_point"
         *               }
         *           }
         *       }
         *   }
         * }
         *
         * 3、设置一个4s店
         * PUT /car_shop/shops/1
         * {
         *     "name": "上海至全宝马4S店",
         *     "pin" : {
         *         "location" : {
         *             "lat" : 40.12,
         *             "lon" : -71.34
         *         }
         *     }
         * }
         */
        //第一个需求:搜索两个坐标点组成的一个区域
        QueryBuilder queryBuilder = geoBoundingBoxQuery("pin.location").setCorners(40.73, -74.1, 40.01, -71.12);
        SearchResponse response = client.prepareSearch("car_shop").setTypes("shops")
                .setQuery(queryBuilder).get();
        for (SearchHit searchHit : response.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("====================================================");
        //第二个需求:指定一个区域,由三个坐标点,组成
        List<GeoPoint> points = new ArrayList<GeoPoint>();
        points.add(new GeoPoint(40.73, -74.1));
        points.add(new GeoPoint(40.01, -71.12));
        points.add(new GeoPoint(50.56, -90.58));

        response = client.prepareSearch("car_shop")
                .setTypes("shops")
                .setQuery(QueryBuilders.geoPolygonQuery("pin.location", points))
                .get();

        for (SearchHit searchHit : response.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("====================================================");
        //第三个需求:搜索距离当前位置在200公里内的4s店
        response = client.prepareSearch("car_shop")
                .setTypes("shops")
                .setQuery(QueryBuilders.geoDistanceQuery("pin.location")
                        .point(40, -70)
                        .distance(200, DistanceUnit.KILOMETERS))
                .get();

        for (SearchHit searchHit : response.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    /**
     * 多种查询
     */
    private static void query() {
        System.out.println("=====================检索===============================");
        SearchResponse searchResponse = client.prepareSearch("car_shop").setTypes("cars")
                .setQuery(QueryBuilders.matchQuery("brand", "宝马")).get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("========================多字段检索============================");
        searchResponse = client.prepareSearch("car_shop").setTypes("cars")
                .setQuery(QueryBuilders.multiMatchQuery("奔驰", "brand", "name")).get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("========================termQuery过虑============================");
        searchResponse = client.prepareSearch("car_shop").setTypes("cars")
                .setQuery(QueryBuilders.termQuery("brand.raw", "华晨宝马")).get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("========================前缀批配============================");
        searchResponse = client.prepareSearch("car_shop").setTypes("cars")
                .setQuery(QueryBuilders.prefixQuery("brand", "宝")).get();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
        System.out.println("========================多种条件的组合搜索========================");
        QueryBuilder queryBuilder = boolQuery().must(matchQuery("brand", "宝马"))
                .mustNot(termQuery("name.raw", "宝马318"))
                .should(termQuery("produce_date", "2017-01-01"))
                .filter(rangeQuery("price").gte("280000").lt("400000000"));
        SearchResponse response = client.prepareSearch("car_shop").setTypes("cars")
                .setQuery(queryBuilder).get();
        for (SearchHit searchHit : response.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    /**
     * es模板调用
     */
    private static void template() {
        Map<String, Object> scriptParams = new HashMap();
        scriptParams.put("from", 0);
        scriptParams.put("size", 1);
        scriptParams.put("brand", "奔驰");
        //在es安装目录下\config\scripts\page_query_by_brand.mustache文件,放入脚本
        /**
         *
         *{
         *   "from": {{from}},
         *   "size": {{size}},
         *   "query": {
         *     "match": {
         *       "brand.keyword": "{{brand}}"
         *     }
         *   }
         * }
         */
        SearchResponse searchResponse = new SearchTemplateRequestBuilder(client).setScript("page_query_by_brand")
                .setScriptType(ScriptType.FILE).setScriptParams(scriptParams)
                .setRequest(new SearchRequest("car_shop").types("cars"))
                .get().getResponse();
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

    /**
     * 使用Scroll批量查询,可以实现文件导出
     */
    private static void scroll() {
        SearchResponse searchResponse = client.prepareSearch("car_shop").setTypes("cars")
                .setQuery(QueryBuilders.termQuery("brand.raw", "奔驰"))//brand.raw是用的不分词类型 keyword
                .setScroll(new TimeValue(60000)).setSize(2).get();//setSize一次查询多少条
        int batchCount = 0;
        do {
            for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                System.out.println("batch:" + ++batchCount);
                System.out.println(searchHit.getSourceAsString());
                // 每次查询一批数据,比如1000行,然后写入本地的一个excel文件中
                // 如果说你一下子查询几十万条数据,不现实,jvm内存可能都会爆掉
            }
            System.out.println("-----------------");
            searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(60000))
                    .execute().actionGet();
        } while (searchResponse.getHits().getHits().length != 0);
    }

    /**
     * bulk实现增删改
     * @throws IOException
     */
    private static void bulk() throws IOException {
        //新增
        BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("car_shop", "cars", "3");
        indexRequestBuilder.setSource(XContentFactory.jsonBuilder().startObject().field("brand", "奔驰")
                .field("name", "奔驰C200")
                .field("price", 350000)
                .field("produce_date", "2017-01-20")
                .field("sale_price", 320000)
                .field("sale_date", "2017-01-25").endObject());
        bulkRequestBuilder.add(indexRequestBuilder);
        //修改
        UpdateRequestBuilder update = client.prepareUpdate("car_shop", "cars", "1").setDoc(
                XContentFactory.jsonBuilder().startObject().field("price", "100000000").endObject()
        );
        bulkRequestBuilder.add(update);
        DeleteRequestBuilder delete = client.prepareDelete("car_shop", "sales", "3");
        bulkRequestBuilder.add(delete);
        BulkResponse bulkResponse = bulkRequestBuilder.get();
        for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
            System.out.println(bulkItemResponse.getResponse());
        }
    }

    /**
     * 一次查多个
     */
    private static void multiGet() {
        MultiGetResponse multiGetResponses = client.prepareMultiGet().add("car_shop", "cars", "1").add("car_shop", "cars", "2").get();
        for (MultiGetItemResponse itemResponse : multiGetResponses) {
            GetResponse response = itemResponse.getResponse();
            if (response.isExists()) {
                System.out.println(response.getSourceAsString());
            }
        }
    }

    /**
     *upsert实现汽车最新价格的调整
     * @throws IOException
     * @throws InterruptedException
     * @throws ExecutionException
     */
    private static void upsert() throws Exception {
        IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "2").source(
                XContentFactory.jsonBuilder()
                        .startObject().field("brand", "宝马")
                        .field("name", "宝马c200")
                        .field("price", 350000)
                        .field("produce_date", "2017-01-01")
                        .endObject());
        System.out.println("index end:");
        UpdateRequest updateRequest = new UpdateRequest("car_shop", "cars", "1")
                .doc(XContentFactory.jsonBuilder()
                        .startObject().field("price", 310000).endObject()).upsert(indexRequest);
        UpdateResponse updateResponse = client.update(updateRequest).get();
        System.out.println(updateResponse.getVersion());
    }

}
这篇关于elasticsearch java api的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!