简体   繁体   English

Elasticsearch NEST 查询的 LINQ 表达式

[英]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参考[我之前的问题及其答案]: 使用 NEST 进行弹性搜索 - 结果在调试和浏览器模式下不同

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 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. CreateIndexDecriptor<T>是用于创建索引的描述符,但您需要将其传递给IElasticClient.CreateIndex()方法以便在 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对 Elasticsearch 的请求看起来像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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