gpt4 book ai didi

spring-boot - 如何在Spring Boot中编写以下 Elasticsearch 通配符查询?

转载 作者:行者123 更新时间:2023-12-03 01:18:15 27 4
gpt4 key购买 nike

数据库:Elasticsearch

字段名称:“标签”

数据类型:字符串

问题:如何在tag字段中使用通配符进行搜索?

我尝试了以下方法(在Kibana中):

用户请求:{ "tag" : [ "Attendance", "Employee" ] }

POST test/_search
{
"query":{
"bool" : {
"should" : [
{"wildcard":{"tag.keyword": "*Attendance*" }},
{"wildcard":{"tag.keyword": "*Employee*" }}
]
}
}
}

这成功地工作了,但我不知道如何在Spring Boot中执行相同的操作。

我从事以下工作:
.should(wildcardQuery("tag.keyword", "Attendance"))

但这只是一个 Realm ,
我需要一个动态字段,其中用户输入的大小和值应该不同。

有人可以指导我吗?

最佳答案

您可以使用如下所示的内容:

休息高级客户查询:

SearchRequest searchRequest = new SearchRequest(<your-index-name>);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder query = QueryBuilders.boolQuery().should(new WildcardQueryBuilder("tag.keyword", "*Attendance*"))
.should(new WildcardQueryBuilder("tag.keyword", "*Employee*"));
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

这里 client是我在类中 Autowiring 的RestHighLevelClient bean,如下所示:
@Autowired
private RestHighLevelClient client;

我在配置类中定义的这个bean为:
@Bean(destroyMethod = "close")
public RestHighLevelClient client() {

RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));

return client;

}

这将以这种形式构造查询:

询问
    {
"bool" : {
"should" : [
{
"wildcard" : {
"tag.keyword" : {
"wildcard" : "*Attendance*",
"boost" : 1.0
}
}
},
{
"wildcard" : {
"tag.keyword" : {
"wildcard" : "*Employee*",
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}

我已经通过创建具有与您提到的相同映射的索引进行了测试:

索引文件:
  "_source": {
"tag": "Attendance1"
}


"_source": {
"tag": "1Employee"
}



"_source": { "tag": "*Employee*" }


{ "tag" : [ "Attendance", "Employee" ] }

当我使用上述rest查询搜索时,得到以下响应:

响应
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "4",
"_score": 2.0,
"_source": {
"tag": [
"Attendance",
"Employee"
]
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"tag": "Attendance1"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"tag": "*Employee*"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"tag": "1Employee"
}
}
]

不知道这部分问题意味着什么:
I have requirement for dynamic field where user input should be different in size and value

但是,我假设这意味着从用户那里您可能会获得不同的 tag列表,并基于该列表创建查询。

您可以执行以下操作以达到相同目的:
        // { "tag" : [ "Attendance", "Employee" ] } you can read this tag list though converter and pass it into list kind of data structure. Currently, I have created dummy list `tags` which has Attendance and Employee as items.
List<String> tags = new ArrayList<>();
tags.add("Attendance");
tags.add("Employee");

SearchRequest searchRequest = new SearchRequest(<your-index-name>);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
List<WildcardQueryBuilder> wildcards = new ArrayList<WildcardQueryBuilder>();

for(String tag : tags) {
WildcardQueryBuilder wildcard = new WildcardQueryBuilder("tag.keyword", "*" + tag + "*");
wildcards.add(wildcard);

}
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
for(WildcardQueryBuilder wildcard : wildcards) {
boolQuery.should(wildcard);
}

searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

关于spring-boot - 如何在Spring Boot中编写以下 Elasticsearch 通配符查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61459194/

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