elasticsearch中的SearchPhaseExecutionException

时间:2016-08-04 10:04:33

标签: java elasticsearch

enter image description here"

我正在使用Elasticsearch 1.7.5 创建索引名称" end"我使用下面的java代码段代码来搜索名为geo_ip的字段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);

任何人都可以帮我解决这个问题吗? 感谢。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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();