简体   繁体   中英

LINQ expressions for Elasticsearch NEST query

I setting up an index as below. But now I have a requirement because of which i need to tweak the indexing style. (i have to add analyzer field in the below code).

Reference[My previous question and its answer]: Elastic Search using NEST - Results different in debug and browser mode

How can I rewrite

var connectionSettings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex)
        .MapDefaultTypeNames(m => m.Add(typeof(Class1), "omg"))
        .PrettyJson()
        .DisableDirectStreaming());

with the mapping settings like below.

{
  "mappings": {
    "Class1": {
      "properties": {
        "Answer": {
          "type": "string",
          "analyzer": "english"
        }
      }
    }
  }
}

This is my take on answer:

 settings = new ConnectionSettings(pool)
              .DefaultIndex(defaultIndex)
              .MapDefaultTypeNames(m => m.Add(typeof(Class1), "omg"))
              .PrettyJson()
              .DisableDirectStreaming();

            var descriptor = new CreateIndexDescriptor(defaultIndex)
                            .Mappings(ms => ms
                            .Map<Class1>(m => m
                            .Properties(ps => ps
                            .String(s=>s
                            .Name(n=>n.Ans)
                            .Analyzer("english")))));

I think I'm missing a link somewhere between the index creation and mappings. Though it didnt show an error while coding, the output is not as expected.

TIA

A CreateIndexDecriptor<T> is a descriptor for creating an index, but you need to pass it to the IElasticClient.CreateIndex() method in order to create the index in Elasticsearch.

void Main()
{
    var defaultIndex = "default-index";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool, new InMemoryConnection())
             .DefaultIndex(defaultIndex)
             .MapDefaultTypeNames(m => m.Add(typeof(Class1), "omg"))
             .PrettyJson()
             .DisableDirectStreaming();

    var client = new ElasticClient(settings);

    client.CreateIndex("new-index", c => c
        .Mappings(ms => ms
            .Map<Class1>(m => m
                .Properties(ps => ps
                    .String(s => s
                        .Name(n => n.Ans)
                        .Analyzer("english")
                    )
                )
            )
        )
    );
}

public class Class1
{
    public string Ans { get; set;}
}

The request to Elasticsearch looks like

{
  "mappings": {
    "omg": {
      "properties": {
        "ans": {
          "type": "string",
          "analyzer": "english"
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM