elasticsearch方面列表与结果不匹配

llmtgqce  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(275)

问题

当我按一个特定的方面过滤时,该特定字段的方面在结果中被正确过滤,但是其他方面字段保持不变。最好的解释方法是查询和响应。

查询

{
    query: {
        match_all: {}
    }, 
    filter: {
        and: [{
            term: {
                "address.state": "oregon"
            }
        }]
    }, 
    facets: {
        "address.city": {
            terms: {
                field: "address.city"
            }, 
            facet_filter: {}
        }, 
        "address.state": {
            terms: {
                field: "address.state"
            }, 
            facet_filter: {
                and: [{
                    term: {
                        "address.state": "oregon"
                    }
                }]
            }
        }, 
        "address.country": {
            terms: {
                field: "address.country"
            }, 
            facet_filter: {}
        }
    }
}

结果

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
            {
                "_index": "races",
                "_type": "race",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "id": 6,
                    "name": "Eugene Marathon",
                    "description": "...",
                    "created_at": "2015-05-24T19:41:45.043Z",
                    "updated_at": "2015-05-24T19:41:45.046Z",
                    "address": {
                        "race_id": 6,
                        "id": 7,
                        "line1": null,
                        "line2": null,
                        "city": "Eugene",
                        "state": "oregon",
                        "country": "united_states",
                        "zip": null,
                        "user_id": null,
                        "created_at": "2015-05-24T19:41:45.044Z",
                        "updated_at": "2015-05-24T19:41:45.044Z"
                    },
                    "race_years": []
                }
            }
        ]
    },
    "facets": {
        "address.city": {
            "_type": "terms",
            "missing": 0,
            "total": 7,
            "other": 0,
            "terms": [
                {
                    "term": "long beach",
                    "count": 1
                },
                {
                    "term": "lincoln",
                    "count": 1
                },
                {
                    "term": "las vegas",
                    "count": 1
                },
                {
                    "term": "jackson",
                    "count": 1
                },
                {
                    "term": "eugene",
                    "count": 1
                },
                {
                    "term": "duluth",
                    "count": 1
                },
                {
                    "term": "denver",
                    "count": 1
                }
            ]
        },
        "address.state": {
            "_type": "terms",
            "missing": 0,
            "total": 1,
            "other": 0,
            "terms": [
                {
                    "term": "oregon",
                    "count": 1
                }
            ]
        },
        "address.country": {
            "_type": "terms",
            "missing": 0,
            "total": 7,
            "other": 0,
            "terms": [
                {
                    "term": "united_states",
                    "count": 7
                }
            ]
        }
    }
}

所以你可以看到它返回所有 address.city 面,即使唯一的结果是在尤金。它还返回了7的数字 united_states . 为什么它会返回所有这些额外的方面和不正确的计数?我的rubyMap可以在下面找到。

rubyMap

settings index: {
  number_of_shards: 1,
  analysis: {
    analyzer: {
      facet_analyzer: {
        type: 'custom',
        tokenizer: 'keyword',
        filter: ['lowercase', 'trim']
      }
    }
  }
} do
  mapping do
    indexes :name, type: 'string', analyzer: 'english', boost: 10
    indexes :description, type: 'string', analyzer: 'english'
    indexes :address do
      indexes :city, type: 'string', analyzer: 'facet_analyzer'
      indexes :state, type: 'string'
      indexes :country, type: 'string'
    end
  end
end
ckx4rj1h

ckx4rj1h1#

这是面在过滤器上运行时的正常行为。根据官方文件:
有一个重要的区别要记住。虽然搜索查询同时限制返回的文档和方面计数,但搜索筛选器只限制返回的文档,而不限制方面计数。
在您的例子中,您的查询匹配所有文档(即。 match_all )因此,facet计数也被计算在所有文档中。
将查询更改为this,facet计数将更改(在这种情况下,您不需要 facet_filter 不再):

{
    query: {
        term: {
            "address.state": "oregon"
        }
    }, 
    facets: {
        "address.city": {
            terms: {
                field: "address.city"
            }
        }, 
        "address.state": {
            terms: {
                field: "address.state"
            }
        }, 
        "address.country": {
            terms: {
                field: "address.country"
            }
        }
    }
}

另一件值得注意的事情是facet已经被弃用,并被更强大的聚合所取代。

相关问题