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

需求:

(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、搜索

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值