所有,我正在尝试调用 ES 6.6 中定义的 SearchTemplate。该模板具有分页变量(来自和大小)和我在数组中传递的电子邮件。这也具有自定义脚本逻辑的排序。当我在 kibana 中运行它时,我看不到分页和排序不起作用。我将不胜感激任何帮助使其发挥作用。请参阅下面的详细信息。我使用索引别名搜索了两个索引。
人和访客索引的映射是相同的(只是为了简化示例)
索引映射
PUT _template/person_guest_template
{
"order": 0,
"index_patterns": ["person*","guest*"],
"settings": {
"index": {
"analysis": {
"filter": {
"autoComplete_filter": {
"type": "edge_ngram",
"min_gram": "2",
"max_gram": "20"
}
},
"analyzer": {
"autoComplete": {
"filter": ["lowercase", "asciifolding","autoComplete_filter"],
"type": "custom",
"tokenizer": "whitespace"
},
"default": {
"filter": ["lowercase", "asciifolding"],
"type": "custom",
"tokenizer": "whitespace"
}
}
},
"number_of_shards": "3",
"number_of_replicas": "1"
}
},
"mappings": {
"_doc": {
"dynamic": false,
"properties": {
"firstName": {
"type": "keyword",
"fields": {
"search": {
"type": "text",
"analyzer": "autoComplete",
"search_analyzer": "default"
}
}
},
"lastName": {
"type": "keyword",
"fields": {
"search": {
"type": "text",
"analyzer": "autoComplete",
"search_analyzer": "default"
}
}
},
"email": {
"type": "keyword"
},"email": {
"type": "keyword"
}
}
}
}
}
搜索模板定义
POST _scripts/guest_person_by_email
{
"script": {
"from": "{{from}}{{^from}}0{{/from}}",
"size": "{{size}}{{^size}}5{{/size}}",
"sort": [
{
"_script": {
"order": "asc",
"type": "number",
"script": "return (doc['type'].value == 'person')? 0 : 1;"
}
},
{
"firstName": {
"order": "asc"
}
},
{
"lastName": {
"order": "asc"
}
}
],
"lang": "mustache",
"source": """
{
"query":{
"bool":{
"filter":{
"terms":{
"email":
{{#toJson}}emails{{/toJson}}
}
}
}
}
}
"""
}
}
使用 SearchTemplate 进行搜索
GET guest-person/_search/template
{
"id":"guest_person_by_email",
"params": {
"emails":["rennishj@test.com"]
}
}
样本数据
PUT person/_doc/1
{
"firstName": "Rennish",
"lastName": "Joseph",
"email": [
"rennishj@test.com"
],
"type":"person"
}
使用 NEST 6.6 调用搜索模板
List<string> emails = new List<string>(){"rennishj@test.com"};
var searchResponse = client.SearchTemplate<object>(st => st
.Index("guest-person")
.Id("guest_person_by_email")
.Params(p => p
.Add("emails", emails.ToArray())
.Add("from", 0)
.Add("size", 50)
)
);
观察
- 当我从搜索模板中删除来自、大小和排序逻辑时,它可以工作
- 好像我将 sort 和 from/size 变量放在错误的位置?
我在这里找到了一个类似的帖子https://discuss.elastic.co/t/c-nest-5-search-with-template/104074/2但似乎 GetSearchTemplate 和 PutSearchTemplate 在 NEST 6.x 上已停产
这可以使用搜索模板来完成吗?我们使用一些非常复杂的 NEST 查询,并且正在远离 NEST 并使用搜索模板。