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

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

    
    <dependency>
        <groupId>org.elasticsearchgroupId>
        <artifactId>elasticsearchartifactId>
        <version>1.6.0version>
    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 map = new HashMap();
    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 templateParams = new HashMap();
        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());
        }

你可能感兴趣的:(java,elsearch)