【问题标题】:Defining index templates on ElasticSearch with .NET client使用 .NET 客户端在 ElasticSearch 上定义索引模板
【发布时间】:2021-01-03 19:59:20
【问题描述】:

我在 Docker 集群中运行了 ElasticSearch,我使用它来搜索查询 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. 模板是否还包含搜索后如何返回 _source 的首选项?例如。如果我想始终排除为自动建议功能添加但不想在正常查询中返回的字段?

提前感谢您的帮助

西蒙

【问题讨论】:

    标签: docker elasticsearch nest elasticsearch-net


    【解决方案1】:

    最终使用 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()));
    

    【讨论】:

    • 我仍然需要解决如何确保只有新索引才能获取我的模板,而不是指标或日志索引。如果我将“index_pattern”定义为适用于所有索引的“*”。有没有办法指定我不想将模板应用于以“。”开头的索引? @RussCam
    猜你喜欢
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多