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());
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值