JAVA Clinet使用Json查询ElasticSearch 5.0+

https://discuss.elastic.co/t/search-elasticsearch-with-java-client-using-json-query/74329/14

  最近有使用json,直接查询ElasticSearch的需求,搜索了很长时间的方法,在ElasticSearch社区找到了答案. 不过,官方吐槽最为致命,Elastic Team 的成员都在吐槽这个需求.

        String content = "{\"query\":{\"match_all\":{}}}";
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
        try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
                .createParser(new NamedXContentRegistry(searchModule.getNamedXContents()), content)) {
            searchSourceBuilder.parseXContent(new QueryParseContext(parser));
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
        SearchResponse searchResponse = searchRequestBuilder.setSource(searchSourceBuilder).execute().actionGet();

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.