[英]How to write following elastic-search wildcard query in spring boot?
Database: Elasticsearch数据库:Elasticsearch
Field Name: "tag"字段名称:“标签”
Datatype: String数据类型:字符串
Question: How to perform a search in the tag
field with a wildcard character?问题:如何在带有通配符的tag
字段中执行搜索?
I tried the following (in Kibana):我尝试了以下(在 Kibana 中):
User Request: { "tag": [ "Attendance", "Employee" ] }
用户请求: { "tag": [ "Attendance", "Employee" ] }
POST test/_search
{
"query":{
"bool" : {
"should" : [
{"wildcard":{"tag.keyword": "*Attendance*" }},
{"wildcard":{"tag.keyword": "*Employee*" }}
]
}
}
}
This worked successfully but I don't have an idea how to perform the same thing in spring boot.这很成功,但我不知道如何在 spring 启动中执行相同的操作。
I worked with following:我与以下人员一起工作:
.should(wildcardQuery("tag.keyword", "Attendance"))
But this is just for a single field, I have a requirement for a dynamic field where user input should be different in size and value.但这只是针对单个字段,我需要一个动态字段,其中用户输入的大小和值应该不同。
Can someone please guide me?有人可以指导我吗?
You could use something like this:你可以使用这样的东西:
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);
Here client
is the RestHighLevelClient bean that I have autowired in my class as follows:这里的client
是我在 class 中自动装配的 RestHighLevelClient bean,如下所示:
@Autowired
private RestHighLevelClient client;
and this bean I have defined in my config class as:我在我的配置 class 中将这个 bean 定义为:
@Bean(destroyMethod = "close")
public RestHighLevelClient client() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
return client;
}
This would construct query in this form:这将以这种形式构造查询:
{
"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
}
}
I have tested by creating index with same mapping as mentioned by you:我已经通过创建与您提到的相同映射的索引进行了测试:
Documents indexed:索引的文件:
"_source": {
"tag": "Attendance1"
}
"_source": {
"tag": "1Employee"
}
"_source": { "tag": "*Employee*" }
{ "tag" : [ "Attendance", "Employee" ] }
And when I search using above rest query, I got the following response:当我使用上面的 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"
}
}
]
Not sure what is meant by this part of your question:不确定您问题的这一部分是什么意思:
I have requirement for dynamic field where user input should be different in size and value
. I have requirement for dynamic field where user input should be different in size and value
。
But, I am assuming it means from user you may get different tag
list and based on that you want to create query.但是,我假设这意味着从用户那里你可能会得到不同的tag
列表,并基于你想要创建查询。
You can do something like this to achieve the same:你可以做这样的事情来达到同样的目的:
// { "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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.