/** * 分頁查詢索引中數據 * * @param indexName * @param size * @param form * @return */ public static List<Telegraph> matchAllQuery(String indexName, int size, int from) { QueryBuilder query = QueryBuilders.matchAllQuery(); SearchResponse response = getClient().prepareSearch(indexName).setQuery(query).setFrom(from).setSize(size) .get(); SearchHits htis = response.getHits(); List<Telegraph> ts = new ArrayList<Telegraph>(); Telegraph t; for (SearchHit searchHit : htis) { System.out.println(searchHit.getSourceAsString()); t = new Telegraph(); t.setTitle((String) searchHit.getSourceAsMap().get("title")); t.setContent((String) searchHit.getSourceAsMap().get("content")); t.setAuthor((String) searchHit.getSourceAsMap().get("author")); t.setPubdate((String) searchHit.getSourceAsMap().get("pubdate")); ts.add(t); } return ts; }
測試分頁查詢測試
public static void main(String[] args) { List<Telegraph> list = matchAllQuery("telegraph", 5, 0); for (Telegraph telegraph : list) { System.out.println("title:"+telegraph.getTitle()+",content:"+telegraph.getContent()+",author:"+telegraph.getAuthor()); } }
執行全文查詢的標準查詢:」title「字段中包含」董事「的文檔ui
/** * 根據查詢條件查詢 */ public static void matchQuery() { QueryBuilder query = QueryBuilders.matchQuery("title", "董事"); SearchResponse response = getClient().prepareSearch("telegraph").setQuery(query).setSize(5).get(); SearchHits searchHits = response.getHits(); for (SearchHit searchHit : searchHits) { System.out.println(searchHit.getSourceAsString()); } }
測試spa
public static void main(String[] args) { matchQuery(); }
執行結果:code
{"title":"週五召開董事會會議 審議及批准更新後的一季報","content":"以審議及批准更新後的2018年第一季度報告","author":"中興通信","pubdate":"2018-07-17T12:33:11"} { "title":"董事長、3名公司高管和2名中層人員被公安機關依法審查", "content":"財聯社7月24日訊,長生生物公告稱,董事長及部分高管沒法正常履職,長春市長春新區公安分局對長春長生生產凍幹人用狂犬病疫苗涉嫌違法犯罪案件立案調查,將主要涉案人員公司董事長、3名公司高管和2名中層人員帶至公安機關依法審查", "author":"長生生物" }
同時知足」董事「、」高管「條件orm
/** * 根據查詢條件查詢 */ public static void matchQuery() { QueryBuilder query = QueryBuilders.matchQuery("title", "董事高管").operator(Operator.AND); SearchResponse response = getClient().prepareSearch("telegraph").setQuery(query).setSize(5).get(); SearchHits searchHits = response.getHits(); for (SearchHit searchHit : searchHits) { System.out.println(searchHit.getSourceAsString()); } }
執行結果:索引
{ "title":"董事長、3名公司高管和2名中層人員被公安機關依法審查", "content":"財聯社7月24日訊,長生生物公告稱,董事長及部分高管沒法正常履職,長春市長春新區公安分局對長春長生生產凍幹人用狂犬病疫苗涉嫌違法犯罪案件立案調查,將主要涉案人員公司董事長、3名公司高管和2名中層人員帶至公安機關依法審查", "author":"長生生物" }
查詢」title「或者」content「任一知足條件」銷售「的文檔文檔
/** * 根據查詢條件查詢 */ public static void matchQuery() { //QueryBuilder query = QueryBuilders.matchQuery("title", "董事高管").operator(Operator.AND); QueryBuilder query = QueryBuilders.multiMatchQuery("銷售", "title","content");// SearchResponse response = getClient().prepareSearch("telegraph").setQuery(query).setSize(5).get(); SearchHits searchHits = response.getHits(); for (SearchHit searchHit : searchHits) { System.out.println(searchHit.getSourceAsString()); } }
查詢結果;get
{"title":"寶泰隆:半年報預增140%-156%","content":"公司主要產品焦炭、甲醇銷售量及銷售價格較上年同期有較大的上漲","author":"寶泰隆","pubdate":"2018-07-17T17:16:30"} { "title":"二甲苯銷售報價大漲9.12%", "content":"財聯社7月24日訊,據百川資訊,今日PX(對二甲苯)報價8140元/噸,漲9.12%,丁二烯報價1.33萬元/噸,漲3.1%;維生素E漲2.56%;氧化鉬漲2.4%;電解錳漲2.13%", "author":"財聯社" }