gpt4 book ai didi

java - 用于字段查询的 Elastic search 高级客户端等效项

转载 作者:行者123 更新时间:2023-11-29 02:58:16 25 4
gpt4 key购买 nike

我有一个 Elasticsearch 查询如下。我想将此查询写入 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"]
}
}
}


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

关于java - 用于字段查询的 Elastic search 高级客户端等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59419053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com