创建索引Nest [英] Creating an index Nest

查看:227
本文介绍了创建索引Nest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Elasticsearch Nest API重新创建以下索引?



这是索引的json,包括映射:

  {
settings:{
analysis:{
filter:{
trigrams_filter :{
type:ngram,
min_gram:3,
max_gram:3
}
},
:{
trigrams:{
type:custom,
tokenizer:standard,
filter:[
小写,
trigrams_filter
]
}
}
}
},
mappings:{
数据:{
_all:{enabled:true},
properties:{
text :{
type:string,
analyzer:trigrams
}
}
}
}
}

这是我的尝试:

  var newIndex = client.CreateIndexAsync(indexName,index => index 
.NumberOfReplicas(replicas)
.NumberOfShards(shards)
.Settings(settings => settings
.Add(merge.policy.merge_factor,10)
.Add(search.slowlog.threshold.fetch.warn,1s)
.Add(mapping.allow_type_wrapper,true))
.AddMapping< Object> =>映射
.IndexAnalyzer(trigram)
.Type(string))
);

文档没有提及任何内容?



更新:



找到 var index = new IndexSettings() >

,然后使用字符串文字json添加分析。



index.Add(analysis ,@{json});



哪里可以找到更多类似这样的例子,这样做吗?

解决方案

有两种主要方法可以通过嵌套创建索引文档



这里是直接将索引设置声明为流利词典条目的方式就像你在上面的例子中做的那样,我在本地进行了测试,它产生t他指定与您的JSON相匹配的设置。

  var response = client.CreateIndex(indexName,s => s 
.NumberOfReplicas(replicas)
.NumberOfShards(shards)
.Settings(settings => settings
.Add(merge.policy.merge_factor,10)
.Add(search.slowlog.threshold.fetch.warn,1s)
.Add(mapping.allow_type_wrapper,true)
.Add(analysis.filter。
.Add(analysis.filter.trigrams_filter.min_gram,3)
.Add(analysis.filter.trigrams_filter.max_gram,3 )
.Add(analysis.analyzer.trigrams.type,custom)
.Add(analysis.analyzer.trigrams.tokenizer,standard)
.Add analysis.analyzer.trigrams.filter.0,lowercase)
.Add(analysis.analyzer.trigrams.filter.1,trigrams_filter)

.AddMapping< ;对象>(映射=>映射
.Type(data)
.AllField(af => af.Enabled())
.Properties(prop => b $ b .String (sprop => sprop
.Name(text)
.IndexAnalyzer(trigrams)



);

请注意,NEST还包括使用强类型类创建索引设置的功能。如果我有时间来完成这个工作,我会举一个例子。



希望这有帮助。


How would I recreate the following index using Elasticsearch Nest API?

Here is the json for the index including the mapping:

{
    "settings": {
        "analysis": {
            "filter": {
                "trigrams_filter": {
                    "type":     "ngram",
                    "min_gram": 3,
                    "max_gram": 3
                }
            },
            "analyzer": {
                "trigrams": {
                    "type":      "custom",
                    "tokenizer": "standard",
                    "filter":   [
                        "lowercase",
                        "trigrams_filter"
                    ]
                }
            }
        }
    },
    "mappings": {
        "data": {
        "_all" : {"enabled" : true},
            "properties": {
                "text": {
                    "type":     "string",
                    "analyzer": "trigrams" 
                }
            }
        }
    }
}

Here is my attempt:

var newIndex = client.CreateIndexAsync(indexName, index => index
            .NumberOfReplicas(replicas)
            .NumberOfShards(shards)
            .Settings(settings => settings
                .Add("merge.policy.merge_factor", "10")
                .Add("search.slowlog.threshold.fetch.warn", "1s")
                .Add("mapping.allow_type_wrapper", true))
             .AddMapping<Object>(mapping => mapping
                .IndexAnalyzer("trigram")
                .Type("string"))
 );

The documentation does not mention anything about this?

UPDATE:

Found this post that uses var index = new IndexSettings()

and then adds Analysis with the string literal json.

index.Add("analysis", @"{json});

Where can one find more examples like this one and does this work?

解决方案

There are two main ways that you can accomplish this as outlined in the Nest Create Index Documentation:

Here is the way where you directly declare the index settings as Fluent Dictionary entries. Just like you are doing in your example above. I tested this locally and it produces the index settings that match your JSON above.

    var response = client.CreateIndex(indexName, s => s
      .NumberOfReplicas(replicas)
      .NumberOfShards(shards)
      .Settings(settings => settings
         .Add("merge.policy.merge_factor", "10")
         .Add("search.slowlog.threshold.fetch.warn", "1s")
         .Add("mapping.allow_type_wrapper", true)
         .Add("analysis.filter.trigrams_filter.type", "nGram")
         .Add("analysis.filter.trigrams_filter.min_gram", "3")
         .Add("analysis.filter.trigrams_filter.max_gram", "3")
         .Add("analysis.analyzer.trigrams.type", "custom")
         .Add("analysis.analyzer.trigrams.tokenizer", "standard")
         .Add("analysis.analyzer.trigrams.filter.0", "lowercase")
         .Add("analysis.analyzer.trigrams.filter.1", "trigrams_filter")
       )
       .AddMapping<Object>(mapping => mapping
          .Type("data")
          .AllField(af => af.Enabled())
          .Properties(prop => prop
             .String(sprop => sprop
               .Name("text")
               .IndexAnalyzer("trigrams")
              )
           )
       )
   );

Please note that NEST also includes the ability to create index settings using strongly typed classes as well. I will post an example of that later, if I have time to work through it.

Hope this helps.

这篇关于创建索引Nest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆