elasticsearch - 弹性搜索精确字段值检索
问题描述
现在我遇到了其他问题 - 我怎样才能只选择适合模糊查询的字段值?假设在现场大学中有不同的名称,例如:教育:[麻省理工学院,斯坦福大学,密歇根大学],但我只想选择斯坦福大学。假设我可以对每个模糊查询进行聚合,这将返回所有计数和来自现场教育的所有大学名称。我需要什么 - 只获取与模糊查询匹配的确切值的聚合。假设我对斯坦福大学进行模糊查询,并且现场教育的值为 [MIT, Stanfordddd University, Michigan University],我希望查询只带回“Stanfordddd University”的值,而不是全部三个他们。谢谢!
解决方案
对于此功能,您的字段education
必须是类型nested
,并且您使用inner_hits
功能来检索唯一相关的值。
education
以下是在这种情况下您的字段的示例映射:
映射:
PUT my_index
{
"mappings":{
"mydocs":{
"properties":{
"education": {
"type": "nested"
}
}
}
}
}
样本文件:
POST my_index/mydocs/1
{
"education": [
{
"value": "Stanford University"
},
{
"value": "Harvard University"
}]
}
POST my_index/mydocs/2
{
"education": [
{
"value": "Stanford University"
},
{
"value": "Princeton University"
}]
}
嵌套字段的模糊查询:
POST my_index/_search
{
"query":{
"nested":{
"path":"name",
"query":{
"bool":{
"must":[
{
"span_near":{
"clauses":[
{
"span_multi":{
"match":{
"fuzzy":{
"name.value":{
"value":"Stanford",
"fuzziness":2
}
}
}
}
},
{
"span_multi":{
"match":{
"fuzzy":{
"name.value":{
"value":"University",
"fuzziness":2
}
}
}
}
}
],
"slop":0,
"in_order":false
}
}
]
}
},
"inner_hits":{}
}
}
}
示例响应:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_index": "my_index",
"_type": "mydocs",
"_id": "2",
"_score": 0.6931472,
"_source": {
"education": [
{
"value": "Stanford University"
},
{
"value": "Princeton University"
}
]
},
"inner_hits": {
"name": {
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "my_index",
"_type": "mydocs",
"_id": "2",
"_nested": {
"field": "education",
"offset": 0
},
"_score": 0.6931472,
"_source": {
"value": "Stanford University"
}
}
]
}
}
}
},
{
"_index": "my_index",
"_type": "mydocs",
"_id": "1",
"_score": 0.6931472,
"_source": {
"education": [
{
"value": "Stanford University"
},
{
"value": "Harvard University"
}
]
},
"inner_hits": {
"name": {
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "my_index",
"_type": "mydocs",
"_id": "1",
"_nested": {
"field": "education",
"offset": 0
},
"_score": 0.6931472,
"_source": {
"value": "Stanford University"
}
}
]
}
}
}
}
]
}
}
请注意inner_hits
您会看到仅Stanford University
返回相关/相关文档的部分。
Elasticsearch 默认返回整个文档作为响应。在某种程度上,您可以使用 执行基于字段的过滤_source
,但是它不允许您过滤值。
希望这可以帮助!
推荐阅读
- java - ArthurHub 裁剪图像未转换为位图
- python - 如何在 pytho 的 1 个静态行中替换动态行?
- tomcat - xmlparserv2_sans_jaxp_services-19.8.0.0 jar 和 xmlparserv2 JAR 19.8.0.0 之间的区别
- android - 旋转片段时使用哪个协程范围?我们可以从视图模型中启动协程吗?
- .net - 在文件上传之前检查文件扩展名的变化
- android - 在android中更改音高后保存音频
- vim - 为什么 Vim 在这里放两个缩进?
- docker-compose - 使用docker-compose挂载一个名称为破折号的文件夹?
- reactjs - 反应原生参考 measureLayout
- java - java中的if语句+计算总数