如何在Java中使用JSON进行Elasticsearch查询? [英] How to make an Elasticsearch query with a JSON in Java?

查看:69
本文介绍了如何在Java中使用JSON进行Elasticsearch查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java Elasticsearch 进行Elasticsearch搜索.弹性搜索为Java提供了API,这很酷.

I am trying to make an Elasticsearch search with Java and Elasticsearch. Elastic search provides API for Java and this is cool.

问题是,我希望在Java中创建一个方法,该方法接收一个字符串(应该是一个包含用于搜索的信息的JSON),该字符串反映此对Elasticsearch的HTTP调用

The thing is, I wish to create a method in Java who receives a string (properly, a JSON containing information for searching) who reflects this HTTP call to Elasticsearch

curl -X GET 'http://localhost:9200/geocon/_search?q=first:"Sonya"'

但是我希望这种方法尽可能通用.

But I want this method as much general as possible.

所以问题是:是否可以使用Java API解析并转换1:1 HTTP请求到Elasticsearch?也许只将JSON字符串作为参数提供给Elasticsearch对象?

So the question is: is it possible to parse and translate 1:1 HTTP request to Elasticsearch with Java API? Maybe giving to the Elasticsearch object just the JSON string as parameter?

推荐答案

基本上,您需要执行以下操作:

Basically you need to do something like this:

String query = whatever_query...
SearchResponse response = esClient.prepareSearch(Configuration.PRIMARY_INDEX_NAME)
                    .setQuery(query)
                    .execute()
                    .actionGet();

这篇关于如何在Java中使用JSON进行Elasticsearch查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆