使用 .NET 客户端在 ElasticSearch 上定义索引模板

Defining index templates on ElasticSearch with .NET client

我在 Docker 集群中安装了 ElasticSearch 运行ning,我用它来搜索查询 ES 的 ASP.NET 核心 API。 我想在集群创建时为 ES 定义索引映射,运行在我的 docker-compose 中创建一个作业,它使用 .NET 控制台应用程序将请求放在 ES 上以定义映射。

我已经看到使用模板应该可以为我完成这项工作,但我在 NEST 或 ElasticSearch-NET 中找不到任何最佳实践示例,所以我想我应该在这里问一下。

我的想法是简单地定义一个应用于创建的任何新索引的模板。然后我 运行 一个为 ES 播种的外部作业,每当新文档需要进入新索引时,任何新索引都将应用相同的映射。 我还想定义一个所有新索引通用的别名。

我要定义的映射如下:

{
    "settings": {
      "analysis": {
        "filter": {
          "autocomplete_filter": {
            "type": "edge_ngram",
            "min_gram": 1,
            "max_gram": 20
          }
        },
        "analyzer": {
          "autocomplete": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "autocomplete_filter"
            ]
          }
        }
      }
    },
    "mappings": {
      "dynamic": false,
      "properties": {
        "Name": {
          "type": "keyword"
        },
        "Cusip": {
          "type": "keyword"
        },
        "ISIN": {
          "type": "keyword"
        },
        "Ticker": {
          "type": "keyword"
        },
        "suggest": {
          "type": "completion",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        }
      }
    }
}

对于别名,我需要使用这样的东西:

{
          "actions" : [
              { "add" : { "index" : "{index}", "alias" : "product" } }
          ]
      }

问题:

  1. 使用模板是执行此操作的正确方法吗?
  2. 如何打包成模板?
  3. 我如何确保所有新索引都具有这些设置并且它不适用于 ES 创建的指标或其他默认索引?
  4. 模板是否也包含有关如何在搜索后 return _source 的首选项?例如。如果我想始终排除为自动建议功能添加的字段,但我不想 return 在正常查询中编辑?

在此先感谢您的帮助

西蒙

最终使用 LowLevel Elasticsearch-net 客户端解决了这个问题。我有一个配置文件,其中包含模板的 JSON 请求:

  "index_patterns": [
    "*"
  ],
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "dynamic": false,
    "properties": {
      "Name": {
        "type": "keyword"
      },
      "field1": {
        "type": "keyword"
      },
      "field2": {
        "type": "keyword"
      },
      "field3": {
        "type": "keyword"
      },
      "suggest": {
        "type": "completion",
        "analyzer": "autocomplete",
        "search_analyzer": "standard"
      }
    }
  },
  "aliases" : {
    "product" : {}
  }
}

然后我发送模板映射请求:

// Send a PUT request containing the template
var postMapping =
        _client.LowLevel.DoRequest<StringResponse>(HttpMethod.PUT, "_template/product_template", PostData.String(template.ToString()));