现在我遇到了另一个问题-如何只选择适合模糊查询的字段值?假设野外大学有不同的名称,例如:教育:[麻省理工学院,斯坦福大学,米欣根大学],但我只选择斯坦福大学。 假设我可以对每个模糊查询进行汇总,这将返回来自野外教育的所有计数和大学的所有名称。我需要的-仅获取与模糊查询匹配的精确值的聚合。假设如果我对斯坦福大学进行模糊查询,并且现场教育持有[麻省理工学院,斯坦福大学,密歇根大学]的值,我想查询的只是带回“斯坦福大学”的值,而不是全部三个他们。谢谢!
答案 0 :(得分:0)
对于此功能,您的字段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
基于 fields 进行过滤,但是它不允许您过滤值。
希望这会有所帮助!