Search in sources :

Example 1 with Page

use of com.topcom.yzswf.util.Page in project topcom-cloud by 545314690.

the class EsUtilTest method main.

public static void main(String[] args) {
    try {
        String idxName = "test";
        String idxType = "attachment";
        XContentBuilder map = jsonBuilder().startObject().startObject(idxType).startObject("properties").startObject("file").field("type", "attachment").startObject("fields").startObject("title").field("store", "yes").endObject().startObject("file").field("term_vector", "with_positions_offsets").field("store", "yes").endObject().endObject().endObject().endObject().endObject().endObject();
        ElasticSearchService service = new ElasticSearchService("tc-es5", "192.168.1.12", 19300);
        // CreateIndexResponse resp = service.client.admin().indices().prepareCreate(idxName)
        // .setSettings(Settings.builder()
        // .put("index.number_of_shards", 1)
        // .put("index.number_of_replicas",1)
        // ).addMapping(idxType,map)
        // .get();
        // EsIndexFile esIndexFile = new EsIndexFile("D:\\Shiro教程.pdf");
        // IndexResponse resp =  service.insertDataWithPipeline("test","document","attachment", JSONObject.fromObject(esIndexFile).toString());
        // System.out.println(resp.toString());
        ESQueryBuilderConstructor constructor = new ESQueryBuilderConstructor();
        constructor.must(new ESQueryBuilders().queryString("attachment.content:在线会话管理"));
        // 查询返回条数,最大 10000
        constructor.setSize(1);
        // 分页查询条目起始位置, 默认0
        constructor.setFrom(0);
        // constructor.setDesc("pubTime"); //排序
        // 
        Page list = service.search("test", "document", constructor);
        // //            Map<Object, Object> map = service.statSearch("bank", "account", constructor, "state");
        System.out.println(list);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ElasticSearchService(com.topcom.yzswf.service.ElasticSearchService) Page(com.topcom.yzswf.util.Page) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 2 with Page

use of com.topcom.yzswf.util.Page in project topcom-cloud by 545314690.

the class ElasticSearchService method search.

/**
 * 功能描述:查询
 * @param index 索引名
 * @param type 类型
 * @param constructor 查询构造
 */
public Page search(String index, String type, ESQueryBuilderConstructor constructor) {
    List<Map<String, Object>> result = new ArrayList<>();
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
    // 排序
    if (StringUtils.isNotEmpty(constructor.getAsc()))
        searchRequestBuilder.addSort(constructor.getAsc(), SortOrder.ASC);
    if (StringUtils.isNotEmpty(constructor.getDesc()))
        searchRequestBuilder.addSort(constructor.getDesc(), SortOrder.DESC);
    // 设置查询体
    searchRequestBuilder.setQuery(constructor.listBuilders());
    // 返回条目数
    int size = constructor.getSize();
    if (size < 0) {
        size = 0;
    }
    if (size > MAX) {
        size = MAX;
    }
    // 返回条目数
    searchRequestBuilder.setSize(size);
    searchRequestBuilder.setFrom(constructor.getFrom() < 0 ? 0 : constructor.getFrom());
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHists = hits.getHits();
    for (SearchHit sh : searchHists) {
        Map<String, Object> source = sh.getSource();
        source.put("_id", sh.getId());
        result.add(sh.getSource());
    }
    return new Page(result, hits.getTotalHits());
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) Page(com.topcom.yzswf.util.Page) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchHits(org.elasticsearch.search.SearchHits) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap)

Aggregations

Page (com.topcom.yzswf.util.Page)2 ElasticSearchService (com.topcom.yzswf.service.ElasticSearchService)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 HashedMap (org.apache.commons.collections.map.HashedMap)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1