使用.net客户端在elasticsearch上定义索引模板

wh6knrhe  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(360)

我在docker集群中运行了elasticsearch,我使用它来搜索asp.net核心api,该api查询es。我想在集群创建时为es定义索引Map,在docker compose中运行一个作业,该作业使用.net控制台应用程序将请求放在es上以定义Map。
我知道使用模板应该可以帮我完成这项工作,但是我在nest或elasticsearch net中找不到任何最佳实践的例子,所以我想在这里问一下。
我的想法是简单地定义一个模板,应用于任何创建的新索引。然后,我运行一个外部作业,该作业将在新文档需要放入新索引时对任何新索引应用相同的Map。我还想定义一个对所有新索引通用的别名。
我要定义的Map如下:

  1. {
  2. "settings": {
  3. "analysis": {
  4. "filter": {
  5. "autocomplete_filter": {
  6. "type": "edge_ngram",
  7. "min_gram": 1,
  8. "max_gram": 20
  9. }
  10. },
  11. "analyzer": {
  12. "autocomplete": {
  13. "type": "custom",
  14. "tokenizer": "standard",
  15. "filter": [
  16. "lowercase",
  17. "autocomplete_filter"
  18. ]
  19. }
  20. }
  21. }
  22. },
  23. "mappings": {
  24. "dynamic": false,
  25. "properties": {
  26. "Name": {
  27. "type": "keyword"
  28. },
  29. "Cusip": {
  30. "type": "keyword"
  31. },
  32. "ISIN": {
  33. "type": "keyword"
  34. },
  35. "Ticker": {
  36. "type": "keyword"
  37. },
  38. "suggest": {
  39. "type": "completion",
  40. "analyzer": "autocomplete",
  41. "search_analyzer": "standard"
  42. }
  43. }
  44. }
  45. }

对于别名,我需要使用以下内容:

  1. {
  2. "actions" : [
  3. { "add" : { "index" : "{index}", "alias" : "product" } }
  4. ]
  5. }

问题:
使用模板是正确的方法吗?
如何将其打包成模板?
如何确保所有新索引都具有这些设置,并且它不适用于由es创建的度量或其他默认索引?
模板是否也包含搜索后如何返回\u源的首选项?e、 g.如果我想一致地排除为autosuggest特性添加的字段,但我不想在正常查询中返回该字段?
提前谢谢你的帮助
西蒙

pvabu6sv

pvabu6sv1#

最后利用底层elasticsearch网络客户端解决了这一问题。我有一个配置文件,其中包含模板的json请求:

  1. "index_patterns": [
  2. "*"
  3. ],
  4. "settings": {
  5. "analysis": {
  6. "filter": {
  7. "autocomplete_filter": {
  8. "type": "edge_ngram",
  9. "min_gram": 1,
  10. "max_gram": 20
  11. }
  12. },
  13. "analyzer": {
  14. "autocomplete": {
  15. "type": "custom",
  16. "tokenizer": "standard",
  17. "filter": [
  18. "lowercase",
  19. "autocomplete_filter"
  20. ]
  21. }
  22. }
  23. }
  24. },
  25. "mappings": {
  26. "dynamic": false,
  27. "properties": {
  28. "Name": {
  29. "type": "keyword"
  30. },
  31. "field1": {
  32. "type": "keyword"
  33. },
  34. "field2": {
  35. "type": "keyword"
  36. },
  37. "field3": {
  38. "type": "keyword"
  39. },
  40. "suggest": {
  41. "type": "completion",
  42. "analyzer": "autocomplete",
  43. "search_analyzer": "standard"
  44. }
  45. }
  46. },
  47. "aliases" : {
  48. "product" : {}
  49. }
  50. }

我发送了模板Map的请求:

  1. // Send a PUT request containing the template
  2. var postMapping =
  3. _client.LowLevel.DoRequest<StringResponse>(HttpMethod.PUT, "_template/product_template", PostData.String(template.ToString()));
展开查看全部

相关问题