对于此功能,您的字段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 执行基于字段 的过滤,但是它不允许您过滤值。
希望这会有所帮助!