0

I wanna query crateDB through ES client.But failed

I set the es.api.enabled: true in the crate.yml and I used the ES java-rest-high-level-client to connect crateDB via port 4200, but I cannot get any response

public class crateAPI {
    public static void main(String[] args) throws Exception{
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 4200, "http")));
        SearchRequest searchRequest =new SearchRequest();
        SearchSourceBuilder searchSourceBuilder =new SearchSourceBuilder(); 
        searchSourceBuilder.query
                     (QueryBuilders.termQuery("test.news.content","test"));
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest);
        for(SearchHit hit : searchResponse.getHits().getHits()){
            System.out.println(hit.getSourceAsString());
        }
        client.close();
    }
}
Spike
  • 5
  • 2

1 Answers1

0

You probably shouldn't (and soon be deprecated) as advised in their official documentation here

But if you have to in the mean time, Crate will respond to a standard ES URL search like so

http://your-server:4200/*your-index-name*/_search?q=your-field:your-search-value
metase
  • 1,169
  • 2
  • 16
  • 29
  • Thx a lot. But how can i use this URL? via http post request? – Spike Jan 15 '19 at 01:19
  • Hi @Spike this would be a HTTP GET, but you should be able to use any other ES POST as long as its pointing to the correct index in this case *your-index-name* is your index and type that Crate creates by default is "default" – metase Jan 15 '19 at 09:28
  • Hi there, I got responses from an HTTP GET request using the basic java `HttpURLConnection`. But I still cannot get any responses from the ES Client (the code is the same as former one). Does that mean I can only get response via the basic Http request but not the ES client – Spike Jan 23 '19 at 08:54
  • I got it ! I forget to add an attribute `searchRequest.indices("test.news");` when I add this and adjust the `termQuery` to ("content","test"), it worked(ES Client). Thanks a lot!! – Spike Jan 23 '19 at 09:10
  • Glad that worked, but I would suggest thinking about it for the future, as they say they will remove this and if you use other functions of Crate you may find that data gets corrupted. If you don't use Crate via official interface I would suggest switching to ElasticSearch instance instead. Just a thought - good luck – metase Jan 23 '19 at 10:29