我最近开始使用 ElasticSearch。我尝试完成一些用例。我对其中一个有疑问。
我已经用全名索引了一些用户(例如“Jean-Paul Gautier”、“Jean De La Fontaine”)。
我尝试获取所有全名以响应某些查询。
例如,我想要以“J”开头的100个最常见的全名
{
"query": {
"query_string" : { "query": "full_name:J*" } }
},
"facets":{
"name":{
"terms":{
"field": "full_name",
"size":100
}
}
}
}
我得到的结果是全名的所有单词:“Jean”、“Paul”、“Gautier”、“De”、“La”、“Fontaine”。
如何获得“Jean-Paul Gautier”和“Jean De La Fontaine”(所有全名值都以“J”开头)? “post_filter”选项没有这样做,它只限制上面的子集。
- 我必须配置这个 full_name 方面的“工作原理”
- 我必须向当前查询添加一些选项
- 我必须做一些“映射”(目前还很模糊)
谢谢
最佳答案
您只需在该字段上设置 "index": "not_analyzed"
,您就可以在您的 facet 中取回完整的、未修改的字段值。
通常,最好有一个版本的字段未分析(用于构面)和另一个版本(用于搜索)。 “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"
}
}
}
}
}
}
}'
这里我们有两个子字段。与父级同名的将是默认值,因此如果您针对 "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"
}'
然后我可以在每个字段上分面查看返回的内容:
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
}
}
}
}'
然后我得到以下信息:
{
"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
}
]
}
}
}
如您所见,analyzed 字段返回单个单词,小写的标记(当您未指定分析器时,使用 standard analyzer),un-analyzed 子字段返回未修改的原始文本。
这是一个您可以玩的可运行示例: http://sense.qbox.io/gist/7abc063e2611846011dd874648fd1b77450b19a5
关于lucene - ElasticSearch - 返回查询方面的完整值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21384894/