Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

elasticsearch - Creating an index 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Creating an index in older versions

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.

Creating index with NEST 7.x

Please also note that in NEST 7.x CreateIndex method is removed. Use Indices.Create isntead. Here's the example.

_client.Indices
    .Create(indexName, s => s
        .Settings(se => se
            .NumberOfReplicas(replicas)
            .NumberOfShards(shards)
            .Setting("merge.policy.merge_factor", "10")));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...