java 使用elasticsearch 以及复杂查询语句构建

elastcisearch 为java开发了API接口,方便java程序的使用。 
首先引入jar包,需要跟elasticsearch版本对应。下面是maven的引入,也可以下载jar包引入。

    <!-- http://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>1.6.0</version>
    </dependency>

使用这种 方式,可只使用一个client端,不会重复申请客户端

    public static final String CLUSTERNAME ="elasticsearch"; //集群模型
    public static final String INDEX = "base_kb"; //索引名称
    public static final String TYPE = "entity";//类型名称
    public static final String HOST = "10.110.6.43"; //服务器地址
    public static final int  PORT = 9300; //服务端口  TCP为9300 IP为9200

    static Map<String, String> map = new HashMap<String, String>();
    static Settings settings = ImmutableSettings.settingsBuilder().put(map).put("cluster.name",CLUSTERNAME)
                                .put("client.transport.sniff", true).build();

    private static TransportClient client;

    static {
        try {
            Class<?> clazz = Class.forName(TransportClient.class.getName());
            Constructor<?> constructor = clazz.getDeclaredConstructor(Settings.class);
            constructor.setAccessible(true);
            client = (TransportClient) constructor.newInstance(settings);
            client.addTransportAddress(new InetSocketTransportAddress(HOST, PORT));

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }

    public static synchronized TransportClient geTransportClient(){
        return client;
    }

在java中构造复杂json数据是让人非常头疼的一件事,在使用java调用API时,语句其实是已经构造了,因此只需使用模板就可以了,模板的使用请看另一篇博文。

        public static  SearchHits getHits(String mention,String mention_type, String lang){
        Map<String, Object> templateParams = new HashMap<String, Object>();
        templateParams.put("mention_"+mention_type, mention);

        TransportClient client = geTransportClient();
        SearchResponse actionGet = client.prepareSearch("base_kb")
                                        .setTypes("entity")                                     
                                        .setTemplateName("template_" + mention_type + "_" + lang)
                                        .setTemplateType(ScriptService.ScriptType.FILE)
                                        .setTemplateParams(templateParams)                      
//                                      .setQuery( QueryBuilders.termQuery("_id", "2"))
                                        .execute()
                                        .actionGet();
        return actionGet.getHits();     
    }


        public static void main(String[] args) {
        // TODO Auto-generated method stub  
//      
        String mention = "中国";
        String mention_type = "ORG";
        String lang = "cmn";

        SearchHits hits = getHits(mention, mention_type, lang);
//      System.out.println(hits.totalHits());
        for (SearchHit hit : hits.getHits()){ //getHits 的使用         
            System.out.println(hit.getId());
            System.out.println(hit.getFields().get("rs_label_zh").getValue());//这样可以获得属性的值
            System.out.println(hit.getFields().get("f_common.topic.description_zh").getValue());
        }
    原文作者:AndyLizh
    原文地址: https://blog.csdn.net/gebitan505/article/details/81457835
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞