0

enter image description here"

I am using Elasticsearch 1.7.5 After create index name "geo_ip" I use java snippet code belows in order to search field country with the name 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);

But after that, it has problem like this:

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)

Can anyone help me to get around this? Thanks.

2 Answers 2

1

Your country field is probably analyzed, so you need to query with lowercase turkey. Try this instead:

    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();

Or use a match query (with either turkey or Turkey) like this:

    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();
Sign up to request clarification or add additional context in comments.

2 Comments

Can you show me the diffirent between analyzed and not-analyzed. Thanks
1

It is better to use Java Api to build a query: https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/search.html

Your query is also wrong. You should specify a query type like term:

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

2 Comments

I use your sugesstion. SearchResponse response = client.prepareSearch(index) .setTypes(type) .setSearchType(SearchType.DEFAULT) .setQuery(QueryBuilders.termQuery("country", "Turkey")) .setFrom(0).setSize(10000).setExplain(true) .execute() .actionGet(); But it print out length of the "respond" is zero. I think I not work well
What is the result of "curl -XGET host:9200/_cat/shards"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.