字段查询的弹性搜索高级客户端等效项



我有一个弹性搜索查询,如下所示。 我想将此查询写入 JAVA 高级客户端。

{"query":{"query_string":{"query":"*123*","fields":["address"]}}}

谁能帮忙? 我无法在高级客户端中找到等效的字段。

这个怎么样:

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class Main {
public static void main(String[] args) throws Exception {
// Create RestHighClient
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
)
);
// Create search query using builder
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders
.queryStringQuery("*123*")
.field("address"));
// Create SearchRequest
SearchRequest request = new SearchRequest();
request.source(builder);
// Request
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response);
client.close();
}
}


结果是:

{
"took": 12,
"timed_out": false,
"_shards": {
"total": 4,
"successful": 4,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "test-191220",
"_type": "_doc",
"_id": "4dxQIW8BbItIJo_OTPzv",
"_score": 1.0,
"_source": {
"address": "123-456, City, Country",
"phone": "123-456-789",
"name": "John Doe"
}
}
]
}
}


问题的示例数据如下所示:

POST test-191220/_doc
{
"address": "123-456, City, Country",
"phone": "123-456-789",
"name": "John Doe"
}
POST test-191220/_search
{
"query": {
"query_string": {
"query": "*123*",
"fields": ["address"]
}
}
}


您可以查看以下链接以获取更多信息:

  • https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html

相关内容

  • 没有找到相关文章

最新更新