需求:
(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();
}
}