13

我正在使用 ElasticSearch,我想知道是否可以使用 faceting 来检索我的结果中的一些统计信息,更具体地说,是我的结果中提到最多的人。我已经有一个包含该信息的字段。但是现在,当我想按多个单词对它进行分组时,我的构面结果会按术语破坏该字段中的数据。

即:如果用户搜索 John,我想获取诸如

   {
    [...]
    "facets" : {

        "topPeople" : {
        "_type" : "terms",
        "missing" : 0,
        "total" : 1739884,
        "other" : 1705319,
        "terms" : [ {
           "term" : "John Smith",
           "count" : 13954
          }, {
           "term" : "John Snow",
           "count" : 1432
          }, {
           "term" : "John Baird",
           "count" : 770
          }]
       }
   }

相反,ElasticSearch 按术语分解结果并返回如下内容:

   {
    [...]
    "facets" : {

        "topPeople" : {
        "_type" : "terms",
        "missing" : 0,
        "total" : 1739884,
        "other" : 1705319,
        "terms" : [ {
           "term" : "John",
           "count" : 1739884
          }, {
           "term" : "Smith",
           "count" : 13954
          }, {
           "term" : "Snow",
           "count" : 1432
          }]
       }
   }

I read somewhere that if I set the index to not be analyzed, ElasticSearch should return the complete string of words. However, I still want the user to be able to search on the field. I would like to avoid duplicating the field to have a non-analyzed one. Is there any way to get grouping per field with ElasticSearch?

I am currently using the following facet query:

{
 "query" : {
   [...]
 },
 "facets" : {
   "topPeople" : {
     "terms" : {
        "field" : "people",
        "size" : 3
      }
    }
  }
}
4

1 回答 1

14

You're on the right track. You need an index which is not analyzed in order to do what you're asking, but you don't need to sacrifice how the user searches on the field. The answer here (for versions < 1.x) is the Multi Field Type. For your example, you'll want your mapping to look something like this:

    "topPeople" : {
        "type" : "multi_field",
        "fields" : {
            "topPeople" : {"type" : "string", "index" : "analyzed"},
            "raw" : {"type" : "string", "index" : "not_analyzed"}
        }
    }

When you search, you can continue to search on topPeople, but when you facet, you'll facet on topPeople.raw.

于 2013-06-24T14:19:00.513 回答