ElasticSearch最佳入門實踐(七十二)Java 實戰 - 對員工資訊進行復雜的搜尋操作
阿新 • • 發佈:2018-11-18
需求:
(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(); } }