获取ElasticSearch方面将多字段字段内容视为原子术语

Emi*_*lie 13 elasticsearch

我正在使用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
          }]
       }
   }
Run Code Online (Sandbox Code Playgroud)

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

   {
    [...]
    "facets" : {

        "topPeople" : {
        "_type" : "terms",
        "missing" : 0,
        "total" : 1739884,
        "other" : 1705319,
        "terms" : [ {
           "term" : "John",
           "count" : 1739884
          }, {
           "term" : "Smith",
           "count" : 13954
          }, {
           "term" : "Snow",
           "count" : 1432
          }]
       }
   }
Run Code Online (Sandbox Code Playgroud)

我在某处读到如果我将索引设置为不被分析,ElasticSearch应该返回完整的单词串.但是,我仍然希望用户能够在该字段上进行搜索.我想避免重复该字段以获得未经分析的字段.有没有办法用ElasticSearch对每个字段进行分组?

我目前正在使用以下构面查询:

{
 "query" : {
   [...]
 },
 "facets" : {
   "topPeople" : {
     "terms" : {
        "field" : "people",
        "size" : 3
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 14

你走在正确的轨道上.您需要一个未分析的索引才能执行您所要求的操作,但您无需牺牲用户在该字段上搜索的方式.这里的答案(对于版本<1.x)是多字段类型.对于您的示例,您希望映射看起来像这样:

    "topPeople" : {
        "type" : "multi_field",
        "fields" : {
            "topPeople" : {"type" : "string", "index" : "analyzed"},
            "raw" : {"type" : "string", "index" : "not_analyzed"}
        }
    }
Run Code Online (Sandbox Code Playgroud)

当你搜索时,你可以继续搜索topPeople,但是当你面对时,你将会面对topPeople.raw.

  • 现在这是使用fields参数完成的.基本上任何核心字段类型(不包括对象和嵌套)现在都接受字段参数.[供参考](https://www.elastic.co/guide/en/elasticsearch/reference/1.6/_multi_fields.html) (2认同)