如何在Spring Boot中编写以下弹性搜索通配符查询?

问题描述 投票:0回答:1

数据库:Elasticsearch

字段名称:“标签”

数据类型:字符串

问题:如何在带有通配符的'tag'字段中执行搜索?

我尝试了以下操作:(在kibana中)

用户请求:{“标签”:[“出勤”,“雇员”]}

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

这项工作成功完成,但是我不知道如何在春季启动中执行相同的操作。

我与以下人员合作,

。should(wildcardQuery(“ tag.keyword”,“出勤”))

但是这仅适用于单个字段,我对动态字段有要求,用户输入的大小和值应该不同。

有人可以引导我吗?

spring-boot elasticsearch jhipster
1个回答
0
投票

您可以使用类似这样的东西:

Rest高级客户端查询:

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);

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

查询

    {
  "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
  }
}

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

映射

"mappings": {
            "properties": {
                "tag": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }

带索引的文档:

Doc1

"_source": {
        "tag": "Attendance1"
}

Doc2

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

Doc3

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

Doc4

{ "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"
            }
        }
    ]
© www.soinside.com 2019 - 2024. All rights reserved.