弹性搜索方面未返回带有范围查询 [英] Elasticsearch facets not returned with a ranged query

查看:85
本文介绍了弹性搜索方面未返回带有范围查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在存储这样的文件:

  {
title:我的标题,
posted_date:2014-03-04T04:00:12 + 00:00,
category:[cat1,cat2],
}

然后我显示这些与各种边栏过滤器的列表,如类别和日期(按年份,按年份月)。根据用户选择结果查询的选项可能会看起来像:

  {
query: {
range:{
posted_date:{
gte:2014-01-01T00:00:00 + 00:00,
lte :2015-01-01T00:00:00 + 00:00
}
},
已过滤:{
过滤器:{
和:{
过滤器:[
{
term:{
category:cat1
}
}
]
}}}}}

这似乎完全按照预期工作,当我尝试添加方面,所以我可以把几个数字(1)旁边的日期等:

  {
query:{
range:{
posted_date:{
gte:2014-01-01T00:00:00 + 00:00,
lte:2015-01-01T00:00:00 + 00:00
}
},
已过滤:{
过滤器:{
和:{
f il子:[
{
term:{
category:fiction
}
}
]
}
}
}
},
facets:{
bymonth:{
date_histogram:{
field post_date,
间隔:月
}
},
byyear:{
date_histogram:{
:posted_date,
间隔:年
}
}
}
}
pre>

只有在查询中不包含范围时,这才有效。如果范围在那里,我不会得到任何返回的方面。这不是结果。即使我改变了另一个术语,而不是日期,我没有任何东西。



范围查询和方面之间是否有不兼容性?有什么其他的方式这些查询的结构,使它不做我认为应该是什么?

解决方案

我认为您需要在这里 facet_filter

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

您更新的查询将如下所示:

  {
查询:{
已过滤:{
过滤器:{
和:{
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:{
:posted_date,
间隔:年
}
}
}
}
pre>

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:

{
  "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.

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

这篇关于弹性搜索方面未返回带有范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆