ElasticSearch - 返回查询的facet的完整值

pie*_*ard 8 lucene elasticsearch

我最近开始使用ElasticSearch.我尝试完成一些用例.其中一个我有问题.

我用一些用户的全名索引了一些用户(例如"Jean-Paul Gautier","Jean De La Fontaine").

我试图让所有的全名响应一些查询.

例如,我希望100个最常用的全名由"J"开始

{
  "query": {
    "query_string" : { "query": "full_name:J*" } }
  },
  "facets":{
    "name":{
      "terms":{
        "field": "full_name",
        "size":100
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我得到的结果是全名:"Jean","Paul","Gautier","De","La","Fontaine".

如何获得"Jean-Paul Gautier"和"Jean De La Fontaine"(所有full_name值由'J'乞讨)?"post_filter"选项不是这样做的,它只限制上面这个子集.

  • 我必须配置这个full_name方面的"如何工作"
  • 我必须为当前查询添加一些选项
  • 我必须做一些"映射"(目前非常模糊)

谢谢

Slo*_*ens 13

您只需"index": "not_analyzed"在该字段上设置,您就可以在您的方面获得完整的,未修改的字段值.

通常,有一个版本的字段没有被分析(用于分面)而另一个版本(用于搜索)是很好的.该"multi_field"字段类型是对这项有益的.

所以在这种情况下,我可以定义一个映射如下:

curl -XPUT "http://localhost:9200/test_index/" -d'
{
   "mappings": {
      "people": {
         "properties": {
            "full_name": {
               "type": "multi_field",
               "fields": {
                  "untouched": {
                     "type": "string",
                     "index": "not_analyzed"
                  },
                  "full_name": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}'
Run Code Online (Sandbox Code Playgroud)

这里我们有两个子字段.与父项同名的那个将是默认值,因此如果您搜索该"full_name"字段,Elasticsearch将实际使用"full_name.full_name"."full_name.untouched"将为您提供您想要的方面结果.

接下来我添加两个文件:

curl -XPUT "http://localhost:9200/test_index/people/1" -d'
{
   "full_name": "Jean-Paul Gautier"
}'

curl -XPUT "http://localhost:9200/test_index/people/2" -d'
{
   "full_name": "Jean De La Fontaine"
}'
Run Code Online (Sandbox Code Playgroud)

然后我可以在每个字段上查看返回的内容:

curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
   "size": 0,
   "facets": {
      "name_terms": {
         "terms": {
            "field": "full_name"
         }
      },
      "name_untouched": {
         "terms": {
            "field": "full_name.untouched",
            "size": 100
         }
      }
   }
}'
Run Code Online (Sandbox Code Playgroud)

我回过头来看:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0,
      "hits": []
   },
   "facets": {
      "name_terms": {
         "_type": "terms",
         "missing": 0,
         "total": 7,
         "other": 0,
         "terms": [
            {
               "term": "jean",
               "count": 2
            },
            {
               "term": "paul",
               "count": 1
            },
            {
               "term": "la",
               "count": 1
            },
            {
               "term": "gautier",
               "count": 1
            },
            {
               "term": "fontaine",
               "count": 1
            },
            {
               "term": "de",
               "count": 1
            }
         ]
      },
      "name_untouched": {
         "_type": "terms",
         "missing": 0,
         "total": 2,
         "other": 0,
         "terms": [
            {
               "term": "Jean-Paul Gautier",
               "count": 1
            },
            {
               "term": "Jean De La Fontaine",
               "count": 1
            }
         ]
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,分析字段返回单字,低字符标记(当您未指定分析器时,使用标准分析器),未分析的子字段返回未修改的原始文本.

这是一个可以玩的可运行的例子:http: //sense.qbox.io/gist/7abc063e2611846011dd874648fd1b77450b19a5