ElasticSearch 입문 - 검색 (자바 api)

11082 단어
ElasticSearch 입문 - 검색 (자바 api)
package com.qlyd.searchhelper;

import java.util.Map;

import net.sf.json.JSONObject;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.highlight.HighlightField;

import com.qlyd.business.newsinfo.po.NewsInfo;
import com.qlyd.common.QlydConstants;
import com.qlyd.utils.ESTools;
import com.qlyd.utils.JsonUtils;
/**
 * 
 * 
 * @   :
 * @   :ElasticSearchHandler
 * @    : //     
 * @author:   
 * @    :2016 3 22    3:26:52
 * @   :
 * @    :
 * @    :         
 * @version 1.0.0
 *
 */
public class ElasticSearchHandler {
    private Client client = ESTools.getClient();

    public void searcher(String key, String indexId, String type) {
        try {

            //       ,  productindex          productindex
            SearchRequestBuilder searchRequestBuilder = client
                    .prepareSearch(indexId);

            //         ,setTypes("productType1", "productType2","productType3");
            //             
            searchRequestBuilder.setTypes(type);
            //        1.SearchType.DFS_QUERY_THEN_FETCH =      2.SearchType.SCAN
            // =     ,  
            searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
            //        
            searchRequestBuilder
                    .setQuery(QueryBuilders.termQuery("title", key));

            // //           4000-5000 
            //      [4000,5000]     ,         4000    5000   
            // searchRequestBuilder.setFilter(FilterBuilders.rangeFilter("price")
            // .from(4000).to(5000));
            //
            // //     
            // searchRequestBuilder.setFrom(0).setSize(3);

            //             
            searchRequestBuilder.setExplain(true);

            //       
            searchRequestBuilder.addHighlightedField("title");
            searchRequestBuilder
                    .setHighlighterPreTags("");
            searchRequestBuilder.setHighlighterPostTags("");
            //     ,        
            SearchResponse response = searchRequestBuilder.execute()
                    .actionGet();
//          System.out.println(response.toString());
            //          
            SearchHits searchHits = response.getHits();
            SearchHit[] hits = searchHits.getHits();
            // ObjectMapper mapper = new ObjectMapper();
            for (int i = 0; i < hits.length; i++) {
                SearchHit hit = hits[i];
                //             json  
                String json = hit.getSourceAsString();
                //  json            
                // Product product = mapper.readValue(json, Product.class);
                NewsInfo newsInfo = JsonUtils
                        .readToObject(json, NewsInfo.class);
                //         
                Map result = hit.highlightFields();
                //              
                HighlightField titleField = result.get("title");
                //          
                Text[] titleTexts = titleField.fragments();
                System.out.println(titleTexts.toString());
                //  title            
                String title = "";
                for (Text text : titleTexts) {
                    title += text;
                }
                //                      
                newsInfo.setTitle(title);
                //                 
                System.out.println(newsInfo);
            }
            System.out.println("search success ..");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public static void main(String[] args) {
        ElasticSearchHandler esHandler = new ElasticSearchHandler();
        System.out.println("++++++    +++++++");
        esHandler.searcher("    ", QlydConstants.INDEX, QlydConstants.TYPE);
    }
}

좋은 웹페이지 즐겨찾기