首页 > 解决方案 > 弹性搜索精确字段值检索

问题描述

现在我遇到了其他问题 - 我怎样才能只选择适合模糊查询的字段值?假设在现场大学中有不同的名称,例如:教育:[麻省理工学院,斯坦福大学,密歇根大学],但我只想选择斯坦福大学。假设我可以对每个模糊查询进行聚合,这将返回所有计数和来自现场教育的所有大学名称。我需要什么 - 只获取与模糊查询匹配的确切值的聚合。假设我对斯坦福大学进行模糊查询,并且现场教育的值为 [MIT, Stanfordddd University, Michigan University],我希望查询只带回“Stanfordddd University”的值,而不是全部三个他们。谢谢!

标签: elasticsearchsearch

解决方案


对于此功能,您的字段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,但是它不允许您过滤值。

希望这可以帮助!


推荐阅读