2016-08-04 24 views
0

enter image description hereSearchPhaseExecutionException在elasticsearch

我使用Elasticsearch 1.7.5 創建索引名後」 geo_ip「我用Java代碼代碼初級講座,以搜索領域country名爲Turkey

String index = "geo_ip"; 
     String type = "ip"; 
     String field = "country"; 
     String value = "Turkey"; 
     Map<String, String> query = new HashMap<>(); 
     query.put(field, value); 
     // create client 
     TransportClient client = EsLoading.settingElasticSearch(); 
     // searching 
     SearchResponse response = client.prepareSearch(index) 
       .setTypes(type) 
       .setSearchType(SearchType.QUERY_AND_FETCH) 
       .setQuery(query) 
       .setFrom(0).setSize(60).setExplain(true) 
       .execute() 
       .actionGet(); 

     SearchHit[] result = response.getHits().getHits(); 
     System.out.println("Current result: "+result.length); 

但之後,它有這樣的問題:

Exception in thread "main" org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query_fetch], all shards failed; shardFailures {[uVH-OgzjQfuUV8Bg_nViHQ][geo_ip][0]: SearchParseException[[geo_ip][0]: from[0],size[60]: Parse Failure [Failed to parse source [{"from":0,"size":60,"query":{"country":"Turkey"},"explain":true}]]]; nested: QueryParsingException[[geo_ip] [_na] query malformed, no field after start_object]; }{[uVH-OgzjQfuUV8Bg_nViHQ][geo_ip][1]: SearchParseException[[geo_ip][1]: from[0],size[60]: Parse Failure [Failed to parse source [{"from":0,"size":60,"query":{"country":"Turkey"},"explain":true}]]]; nested: QueryParsingException[[geo_ip] [_na] query malformed, no field after start_object]; }{[uVH-OgzjQfuUV8Bg_nViHQ][geo_ip][2]: SearchParseException[[geo_ip][2]: from[0],size[60]: Parse Failure [Failed to parse source [{"from":0,"size":60,"query":{"country":"Turkey"},"explain":true}]]]; nested: QueryParsingException[[geo_ip] [_na] query malformed, no field after start_object]; }{[uVH-OgzjQfuUV8Bg_nViHQ][geo_ip][3]: SearchParseException[[geo_ip][3]: from[0],size[60]: Parse Failure [Failed to parse source [{"from":0,"size":60,"query":{"country":"Turkey"},"explain":true}]]]; nested: QueryParsingException[[geo_ip] [_na] query malformed, no field after start_object]; }{[uVH-OgzjQfuUV8Bg_nViHQ][geo_ip][4]: SearchParseException[[geo_ip][4]: from[0],size[60]: Parse Failure [Failed to parse source [{"from":0,"size":60,"query":{"country":"Turkey"},"explain":true}]]]; nested: QueryParsingException[[geo_ip] [_na] query malformed, no field after start_object]; } 
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:237) 
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:183) 
    at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:565) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

任何人都可以幫我解決這個問題嗎? 謝謝。

回答

1

country場大概分析,所以你需要用小寫turkey查詢。試試這個:

SearchResponse response = client.prepareSearch(index) 
      .setTypes(type) 
      .setSearchType(SearchType.QUERY_AND_FETCH) 
      .setQuery(QueryBuilders.termQuery("country", "turkey")) 
      .setFrom(0).setSize(60).setExplain(true) 
      .execute() 
      .actionGet(); 

或者使用match查詢(帶有turkeyTurkey)是這樣的:

SearchResponse response = client.prepareSearch(index) 
      .setTypes(type) 
      .setSearchType(SearchType.QUERY_AND_FETCH) 
      .setQuery(QueryBuilders.matchQuery("country", "Turkey")) 
      .setFrom(0).setSize(60).setExplain(true) 
      .execute() 
      .actionGet(); 
+0

你能告訴我分析和不分析之間的差異嗎?謝謝 – VanThaoNguyen

+0

這個答案可能有幫助:http://stackoverflow.com/questions/37532648/analyzed-or-not-analyzed-what-to-choose – Val

1

這是更好地使用Java Api構建查詢:https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/search.html

您的查詢也是錯誤的。您應指定查詢類型像term

{ 
    "from" : 0, 
    "size" : 60, 
    "query" : 
    { 
     "term" : 
     { 
      "country" : "Turkey" 
     } 
    }, 
    "explain" : true 
} 
+0

我用你的sugesstion。 SearchResponse響應= client.prepareSearch(指數) .setTypes(類型) .setSearchType(SearchType.DEFAULT) .setQuery(QueryBuilders.termQuery( 「國家」, 「土耳其」)) .setFrom(0).setSize( 10000).setExplain(真) .execute() .actionGet(); 但是它打印出「響應」的長度爲零。我覺得我不太好 – VanThaoNguyen

+0

「curl -XGET host:9200/_cat/shards」的結果是什麼? – alpert