简体   繁体   English

远程查询未返回的Elasticsearch构面

[英]Elasticsearch facets not returned with a ranged query

I'm storing documents that look something like this: 我正在存储看起来像这样的文档:

{
   "title" : "My title",
   "posted_date" : "2014-03-04T04:00:12+00:00",
   "category" : ["cat1", "cat2"],
}

Then I am showing listings of these with various sidebar filters for things like category and date(by year, by month). 然后,我将使用各种侧边栏过滤器显示这些列表,例如类别和日期(按年,按月)。 Depending on the options the user selects the resulting query may end up looking something like: 根据用户选择的选项,结果查询可能最终看起来像:

{
"query": {
  "range": {
    "posted_date": {
      "gte": "2014-01-01T00:00:00+00:00",
      "lte": "2015-01-01T00:00:00+00:00"
    }
  },
  "filtered": {
    "filter": {
      "and": {
        "filters": [
          {
            "term": {
              "category": "cat1"
            }
          }
        ]
   }}}}}

Which seems to work entirely as expected, except when I try to add facets so I can put little numbers (1) next to the dates etc: 这似乎完全按预期工作,除了当我尝试添加构面时,我可以在日期等旁边加上小数字(1)等:

{
  "query": {
    "range": {
      "posted_date": {
        "gte": "2014-01-01T00:00:00+00:00",
        "lte": "2015-01-01T00:00:00+00:00"
      }
    },
    "filtered": {
      "filter": {
        "and": {
          "filters": [
            {
              "term": {
                "category": "fiction"
              }
            }
          ]
        }
      }
    }
  },
  "facets": {
    "bymonth": {
      "date_histogram": {
        "field": "posted_date",
        "interval": "month"
      }
    },
    "byyear": {
      "date_histogram": {
        "field": "posted_date",
        "interval": "year"
      }
    }
  }
} 

This works only if I don't include the range in the query. 仅当我不在查询中包括范围时,此方法才有效。 If the range is in there I don't get anything returned for facets. 如果范围在那儿,我就不会得到任何回报。 It's just not in the result. 只是没有结果。 Even if I change the faceting to be by another term rather than date, I don't get anything. 即使我将刻面更改为另一个术语而不是日期,我也一无所获。

Is there something incompatible between range queries and facets? 范围查询和构面之间是否存在不兼容的问题? Is there something else about the way these queries are structured that makes it not do what I think it should be? 这些查询的结构方式是否还有其他问题,使其无法按照我认为的方式进行?

I think you need here facet_filter . 我认为您需要在这里facet_filter

{
    "facets": {
        "facet1": {
            "terms_stats": {
                "key_field" : "name",
                "value_field": "count"
            },
             "facet_filter": {
                "range": {
                   "filed": {
                      "from": 0,
                      "to": 20
                   }
                }
             }
        }
    }
}

Your updated query will be look like this: 您更新的查询将如下所示:

{
   "query": {
      "filtered": {
         "filter": {
            "and": {
               "filters": [
                  {
                     "term": {
                        "category": "fiction"
                     }
                  }
               ]
            }
         }
      }
   },
   "facets": {
      "bymonth": {
         "date_histogram": {
            "field": "posted_date",
            "interval": "month"
         },
         "facet_filter": {
            "range": {
               "posted_date": {
                  "gte": "2014-01-01T00:00:00+00:00",
                  "lte": "2015-01-01T00:00:00+00:00"
               }
            }
         }
      },
      "byyear": {
         "date_histogram": {
            "field": "posted_date",
            "interval": "year"
         }
      }
   }
}

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

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