ElasticSearch最佳入门实践(七十二)Java 实战 - 对员工信息进行复杂的搜索操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33746789/article/details/84197540

需求:

(1)搜索职位中包含technique的员工
(2)同时要求age在30到40岁之间
(3)分页查询,查找第一页

1、构建员工信息

public class EmployeeSearchApp {

    public static void main(String[] args) throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        createEmployee(client);
        client.close();
    }

    //构建员工信息(创建一个document)
    private static void createEmployee(TransportClient client) throws Exception {
        client.prepareIndex("company", "employee", "2")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("name", "punch")
                        .field("age", 31)
                        .field("position", "moju many")
                        .field("country", "china")
                        .field("join-date", "2017-01-17")
                        .field("salary", 9000).endObject()).get();

        client.prepareIndex("company", "employee", "3")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("name", "jack")
                        .field("age", 35)
                        .field("position", "jixie technique")
                        .field("country", "china")
                        .field("join-date", "2013-02-17")
                        .field("salary", 5000).endObject()).get();

        client.prepareIndex("company", "employee", "4")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("name", "many")
                        .field("age", 40)
                        .field("position", "yinhang technique")
                        .field("country", "china")
                        .field("join-date", "2013-03-17")
                        .field("salary", 11000).endObject()).get();

        client.prepareIndex("company", "employee", "5")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("name", "punny")
                        .field("age", 28)
                        .field("position", "wenyuan technique")
                        .field("country", "china")
                        .field("join-date", "2015-04-17")
                        .field("salary", 3000).endObject()).get();
    }
}

在这里插入图片描述

2、搜索

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33746789/article/details/84197540
今日推荐