【问题标题】:Elasticsearch TermsFacet giving wrong countElasticsearch TermsFacet 给出错误的计数
【发布时间】:2013-03-27 22:26:48
【问题描述】:

我在使用 Elasticsearch Term Facet 时遇到了问题

我把数据如下:

curl -X DELETE "http://localhost:9200/articles'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "One",   "tags" : "foo","datetime":"2005-12-23 23:10:52"}'


curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "Two",   "tags" : "bar","datetime":"2005-12-23 23:10:53"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "Three", "tags" : "baz","datetime":"2005-12-23 23:10:54"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "four", "tags" : "baz","datetime":"2005-12-23 23:10:55"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "five", "tags" : "foo","datetime":"2005-12-23 23:10:56"}'

所以每当我查询术语方面时,它都会给出正确的结果,以下是我的 Elasticsearch 查询:

curl  'http://localhost:9200/articles/article/_search?pretty=true' -d '{

   "query": {
       "match_all": {}
   },
    "facets" : { "myfacet" : { "terms" : {"field" : "tags"}}
    }
}'

但是,当我将过滤器添加到 Facet 时,它不会显示任何方面计数以下是查询:

curl  'http://localhost:9200/articles/article/_search?pretty=true' -d '{

   "query": {
       "match_all": {}
   },
   "facets" : {
        "myfacet" : { "terms" : {"field" : "tags"},
             "filter" : { "range" :{
 "datetime" : {"from" : "2005-12-23   3:10:52","to" : "2005-12-23 23:10:56" }

        }
            }
    }
    }
}'

我得到如下结果

facets" : {
    "myfacet" : {
      "_type" : "filter",
      "count" : 0
    }
  }

所以,任何人都知道它为什么会这样计数。

【问题讨论】:

  • 嗨 uttamp,你有机会试试我的答案吗?

标签: elasticsearch


【解决方案1】:

日期格式无效,看一下elasticsearch支持的日期时间格式(太长了,别看了,jodatime支持的任何日期都是elasticsearch支持的)。

http://www.elasticsearch.org/guide/reference/mapping/date-format.html

话虽如此,您只需要修改插入语句中的日期并将它们放入有效的日期格式,例如2005-12-23T23:10:55Z。然后只需将您的查询更改为该时间格式的适当时间范围,这应该会给您结果。

在编写这些查询时也要小心,因为我注意到您在 from 子句中使用的日期无效。

以下是修改后的 curl 脚本:

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "One",   "tags" : "foo","datetime":"2005-12-23T23:10:52Z"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "Two",   "tags" : "bar","datetime":"2005-12-23T23:10:53Z"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "Three", "tags" : "baz","datetime":"2005-12-23T23:10:54Z"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "four", "tags" : "baz","datetime":"2005-12-23T23:10:55Z"}'

curl -X POST "http://localhost:9200/articles/article" -d '{"title" : "five", "tags" : "foo","datetime":"2005-12-23T23:10:56Z"}'

以及修改后的搜索:

 curl  'http://localhost:9200/articles/article/_search?pretty=true' -d '{

   "query": {
      "match_all": {}
    },
    "facets" : {
     "myfacet" : { 
         "terms" : {"field" : "tags"},
         "filter" : { "range" :{
                         "datetime" : {
                             "from" : "2005-12-23T23:10:52Z",
                              "to" : "2005-12-23T23:10:54Z" 
                          }   
                       }
                    }
                  }
             }
}'

希望这会有所帮助, 马特

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2015-04-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多