简体   繁体   English

Elasticsearch方面列表与结果不匹配

[英]Elasticsearch Facet List doesn't Match Results

Problem 问题

When I filter by a particular facet, that specific field's facets are correctly filtered in the result but the other facet fields remain the same. 当我按特定构面筛选时,该特定字段的构面会在结果中正确过滤,但其他构面字段保持不变。 Best way to explain this is with the query and the response. 最好的解释方法是查询和响应。

Query 询问

{
    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: {}
        }
    }
}

Result 结果

{
    "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
                }
            ]
        }
    }
}

So as you can see it returns all the address.city facets even though the only result is located in Eugene. 因此,您可以看到,即使唯一的结果位于Eugene中,它也会返回所有address.city方面。 It is also returning a count of 7 on the united_states . 它还在united_states返回7的计数。 Why would it be returning all of these extra facets and with incorrect counts? 为什么它会返回所有这些额外方面并且计数错误? My ruby mapping is found below. 我的红宝石映射如下。

Ruby Mapping Ruby映射

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

This is the normal behavior of facets when ran against a filter. 当遇到过滤器时,这是构面的正常行为。 From the official documentation : 官方文档中

There's one important distinction to keep in mind. 要记住一个重要的区别。 While search queries restrict both the returned documents and facet counts, search filters restrict only returned documents — but not facet counts. 虽然搜索查询同时限制了返回的文档和构面数量,但是搜索过滤器仅限制了返回的文档,而没有构面数量。

In your case, your query matches all documents (ie match_all ) so the facet counts are counted against all documents, too. 在您的情况下,您的查询将匹配所有文档(即match_all ),因此构面计数也将针对所有文档进行计数。

Change your query to this and your facet counts will change (in this case you don't need the facet_filter anymore): 将查询更改为此,您的构面计数将发生变化(在这种情况下,您不再需要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"
            }
        }
    }
}

Another thing worth noting is that facets are deprecated and have been replaced by the much more powerful aggregations . 另一点值得注意的是, 各个方面已被弃用,并已被功能更强大的聚合所取代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM