ElasticSearchクエリーテンプレート

14505 ワード

必要なjarパッケージ、私はここでmavenのpom.xmlを使ってインポートして、自分でjarパッケージをダウンロードしてプロジェクトにインポートすることができます
                
                        org.elasticsearch.client
                        transport
                        5.4.2
                
		
			org.elasticsearch
			elasticsearch
			5.4.2
		

クラスタClientConnect.javaの接続
import java.net.InetSocketAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

/**
 * 
 * @createDate 2018 3 8 
 * @class_name ClientConnect   ES  
 * @description :   es client,  getClient()  ,    es5.4.2
 */
public class ClientConnect {
	private TransportClient client;

	public TransportClient getClient() {
		return client;
	}

	/**
	 * 
	 * @param clusterName           
	 * @param clusterAddress      ip  
	 * @param port                 
	 */
	@SuppressWarnings({ "unchecked", "resource" })
	public ClientConnect(String clusterName, String clusterAddress, int port) {
		Settings settings = Settings.builder().put("cluster.name", clusterName)
				.build();
		client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress(
						new InetSocketAddress(clusterAddress, port)));
		System.out.println("success connect");
	}

}

クエリーテンプレートBaseSearch.java
package util;

import java.util.Arrays;
import java.util.Iterator;

import org.apache.lucene.search.Sort;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction.Modifier;
import org.elasticsearch.index.query.MultiMatchQueryBuilder.Type;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;


	/**
	 * @date 2018 3 8 
	 * @class_name baseSearch
	 * @description :     
	 */
public class BaseSearch {
	private TransportClient client;

	private SearchResponse response;
	private SearchRequestBuilder searchRequestBuilder;
	
	/**
	 * @description :         
	 * @param client	es    ip  
	 */
	public BaseSearch(TransportClient client){
		this.client=client;
	}
	
	/**
	 * @description :              
	 */
	private QueryBuilder setQuery(){
        /*	
         * 	must		    must      ,         score
         *	filter		    must      ,          ,     
	 *	should		    should      ,    should                 ,         minimum_should_match            
	 *	must_not	              ,  filter   ,       ,    
         * 
         * 1.       ,   "title","summary","author","content"            "   ",          1.0 。
         * 2.       ,   "title","summary"           "      ",         ,           2 
         * 3.       ,   "title","summary"          "   ",             ,    "AUTO"
         * 4.      "title"            "  ","  ","  "       ,           -0.9 
         * 5.       "title","summary","author","content"            "  "
         * 6.	     ,   "author"           "*w*",  ?       ,*         
         * 7.       ,   "author"          "[a-z]k[a-z]"
         * 8.	      ,   "summary"    " "     
         * 9-11。   3 should           
         * 12. 	            ,      ,    
         * 13.     "publish_time"       from("2018-01-13 14:21:09") to("2018-01-17 14:21:09")  
         * 14.	    "title"  
         * 15.      DecayFunction(field, origin, scale, offset, decay), linear,gauss,exponential  ,   origin      
         * 16.      fieldValueFactorFunction(field).    field          
         */
		return QueryBuilders
					.boolQuery()
		/*1*/	//	.must(QueryBuilders.multiMatchQuery("   ", "title","summary","author","content").boost(1.0f))
		/*2*/	//	.must(QueryBuilders.multiMatchQuery("      ", "title","summary").type(Type.PHRASE).slop(2))
		/*3*/	//	.must(QueryBuilders.multiMatchQuery("  ", "title","summary").fuzziness("AUTO"))
		/*4*/	//	.must(QueryBuilders.termsQuery("title", "  ","  ","  ").boost(-0.9f))
		/*5*/	//	.mustNot(QueryBuilders.multiMatchQuery("  ", "title","summary","author","content"))
		/*6*/	//	.must(QueryBuilders.wildcardQuery("author", "*w*"))
		/*7*/ 	//	.must(QueryBuilders.regexpQuery("author", "[a-z]k[a-z]"))
		/*8*/ 	//	.must(QueryBuilders.matchPhrasePrefixQuery("summary"," "))
		/*9*/	//	.should(QueryBuilders.multiMatchQuery("  ","title","summary","author","content"))
		/*10*/	//	.should(QueryBuilders.multiMatchQuery("  ","title","summary","author","content"))
		/*11*/	//	.should(QueryBuilders.multiMatchQuery("  ","title","summary","author","content"))
		/*12*/	//	.minimumShouldMatch("55%")
		/*13*/	//	.must(QueryBuilders.rangeQuery("publish_time").from("2018-01-13 14:21:09").to("2018-01-17 14:21:09"))//50.45 59.12  1741 1749.66
		/*14*/  //	.must(QueryBuilders.existsQuery("title"))
		/*15*/	//	.must(QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.linearDecayFunction("price",67,20,5,0.5)))
		/*16*/	//	.must(QueryBuilders.functionScoreQuery(ScoreFunctionBuilders.fieldValueFactorFunction("comment").modifier(Modifier.LN1P).factor(0.1f)).boostMode(CombineFunction.SUM))
		;
	}
	
	/**
	 * @description :              ,       ,
	 */
	private QueryBuilder setFilter(){
		/*	
         * 	must		    must      ,         score
	 *	filter		    must      ,          ,     
	 *	should		    should      ,    should                 ,         minimum_should_match            
	 *	must_not	              ,  filter   ,       ,    
         * 
         * 1.       ,   "title","summary","author","content"            "   ",          1.5 。
         * 2.       ,   "title","summary"           "      ",         ,           2 
         * 3.       ,   "title","summary"          "   ",             ,    "AUTO"
         * 4.      "title"            "  ","  ","  "       ,           -0.9 
         * 5.       "title","summary","author","content"            "  "
         * 6.	     ,   "author"           "*w*",  ?       ,*         
         * 7.       ,   "author"          "[a-z]k[a-z]"
         * 8.	      ,   "summary"    " "     
         * 9-11。	   3 should           
         * 12. 	            ,      ,    
         * 13.     "publish_time"       from("2018-01-13 14:21:09") to("2018-01-17 14:21:09")  
         */
		return QueryBuilders
					.boolQuery()
		/*1*/	//	.must(QueryBuilders.multiMatchQuery("   ", "title","summary","author","content").boost(1.5f))
		/*2*/	//	.must(QueryBuilders.multiMatchQuery("      ", "title","summary").type(Type.PHRASE).slop(2))
		/*3*/	//	.must(QueryBuilders.multiMatchQuery("  ", "title","summary").fuzziness("AUTO"))
		/*4*/	//	.must(QueryBuilders.termsQuery("title", "  ","  ","  ").boost(-0.9f))
		/*5*/	//	.mustNot(QueryBuilders.multiMatchQuery("  ", "title","summary","author","content"))
		/*6*/	//	.must(QueryBuilders.wildcardQuery("author", "*w*"))
		/*7*/ 	//	.must(QueryBuilders.regexpQuery("author", "[a-z]k[a-z]"))
		/*8*/ 	//	.must(QueryBuilders.matchPhrasePrefixQuery("summary"," "))
		/*9*/	//	.should(QueryBuilders.multiMatchQuery("  ","title","summary","author","content"))
		/*10*/	//	.should(QueryBuilders.multiMatchQuery("  ","title","summary","author","content"))
		/*11*/	//	.should(QueryBuilders.multiMatchQuery("  ","title","summary","author","content"))
		/*12*/	//	.minimumShouldMatch("55%")
		/*13*/	//	.must(QueryBuilders.rangeQuery("publish_time").from("2018-01-13 14:21:09").to("2018-01-17 14:21:09"))
		 			;
	}
	
	/**
	 * @description :                
	 */
	private HighlightBuilder setHighlight(){
		HighlightBuilder highlightBuild = new HighlightBuilder()
				//         
				.field("summary").field("title")
				//        
				.preTags("").postTags("");
		return highlightBuild;
	}
	
	/**
	 * @param response 
	 * @description :       
	 */
	public void printResponse(){
		response=this.getResponse();
		// timed_out
        boolean isTimedOut = response.isTimedOut();
        System.out.println("    ,timed_out:"+isTimedOut);
        
        // _shards,      ,      ,      
        int totalShards = response.getTotalShards();
        int successfulShards = response.getSuccessfulShards();
        int failedShards = response.getFailedShards();
        System.out.println("     :{ total="+totalShards+" successful="+successfulShards+" failed="+failedShards+"},   "+response.getHits().totalHits+"   ");
        
        //    hit   ,        API SearchHits
        SearchHits searchHits = response.getHits();
        Iterator iterator = searchHits.iterator();
        while(iterator.hasNext()) {
            SearchHit hit = iterator.next();
            String hitIndex = hit.getIndex();
            String hitType = hit.getType();
            String id = hit.getId();
            float score = hit.getScore();
            System.out.println("index="+hitIndex+" type="+hitType+" id="+id+" score="+score+" source-->"+hit.getSourceAsString());
            System.out.println(hit.getHighlightFields());
        }
        System.out.println("    ...");
	}
	

	/**
	 * @description       ,    BasaSearch    setQuery(),setFilter(),setHighlight()
	 * @param indexs     
	 * @param types	    
	 */
	public void getSearchByIndexAndType(String[] indexs,String[] types) {
		
        System.out.print("        "+Arrays.deepToString(indexs)
        		+"    "+Arrays.deepToString(types)
        		+"     ");
        //  
        
        QueryBuilder queryBulid=this.setQuery();
        QueryBuilder filterBulid=this.setFilter();
        HighlightBuilder highlightBuild1=this.setHighlight();
        
		searchRequestBuilder =       				
        		//     index, {"twitter","indexName"}		
        		client.prepareSearch(indexs)
        		//     type, {"tweet","typeName"}
                .setTypes(types)
                /*       
                * 1.SearchType.DFS_QUERY_THEN_FETCH =    
                * 2.SearchType.QUERY_THEN_FETCH =    
                */
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                //    
                .setQuery(queryBulid)
                //     
                .setPostFilter(filterBulid)  
                //           		
                .setFrom(0)
                //           
                .setSize(50)
                //            ,    ,        ,          
//              .setExplain(true)
                //      ,
                .highlighter(highlightBuild1)
                //    "publish_time"  
//              .addSort("publish_time", SortOrder.DESC)
                ;
				
		System.out.println("    ...");
		//    ,     
		SearchResponse response = searchRequestBuilder.get();
		this.setResponse(response);
    }
	
	/**
	 * @description :             JSON  
	 * @return String
	 */
	public String getRequestJSON(){
		return searchRequestBuilder.toString();
	}
	
	public TransportClient getClient() {
		return client;
	}
	public void setClient(TransportClient client) {
		this.client = client;
	}
	
	public SearchRequestBuilder getSearchRequestBuilder() {
		return searchRequestBuilder;
	}
	public void setSearchRequestBuilder(SearchRequestBuilder searchRequestBuilder) {
		this.searchRequestBuilder = searchRequestBuilder;
	}
	
	public SearchResponse getResponse() {
		return response;
	}
	public void setResponse(SearchResponse response) {
		this.response = response;
	}
}

テストインスタンスSearchTest.java
package instancetest;

import org.elasticsearch.client.transport.TransportClient;

import util.BaseSearch;
import util.ClientConnect;

public class SearchTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TransportClient client = new ClientConnect("elasticsearch", "192.168.203.201", 9320).getClient();
		BaseSearch bs = new BaseSearch(client);
		String[] index = {};
		String[] type = {};
		//  index type   ,        
		bs.getSearchByIndexAndType(index, type);
		//         
		bs.printResponse();
		//        
		System.out.println(bs.getRequestJSON());

	}

}

必要に応じてクエリーテンプレートBaseSearch.javaを変更して、検索結果を取得します.
様々な試みを行うことでElasticSearchのクエリー機能を身につける.
注意:使用するElasticSearchのjarパッケージは、クラスタで使用されているバージョンと同じでなければなりません.そうしないと、クラスタに接続できないエラーが発生する可能性があります.