Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么控制器中的Nest方法创建索引?_C#_Asp.net_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Model View Controller_Nest - Fatal编程技术网 elasticsearch,model-view-controller,nest,C#,Asp.net,elasticsearch,Model View Controller,Nest" /> elasticsearch,model-view-controller,nest,C#,Asp.net,elasticsearch,Model View Controller,Nest" />

C# 为什么控制器中的Nest方法创建索引?

C# 为什么控制器中的Nest方法创建索引?,c#,asp.net,elasticsearch,model-view-controller,nest,C#,Asp.net,elasticsearch,Model View Controller,Nest,我正在使用NET6.0.1和asp.NETMVC var node = new Uri("http://localhost:9200"); var setting = new ConnectionSettings(node); var client = new ElasticClient(setting); var news = new News { NewsTitle = "

我正在使用NET6.0.1和asp.NETMVC

var node = new Uri("http://localhost:9200");
        var setting = new ConnectionSettings(node);
        var client = new ElasticClient(setting);

        var news = new News
        {
            NewsTitle = "TestTitle"
            
        };

        client.Index(news, idx => idx.Index("NewsTitle"));
        var response = client.Get<News>(1, idx => idx.Index("NewsTitle"));
var节点=新Uri(“http://localhost:9200");
var设置=新连接设置(节点);
var客户端=新的ElasticClient(设置);
var新闻=新新闻
{
newsttitle=“TestTitle”
};
client.Index(news,idx=>idx.Index(“newsttitle”);
var response=client.Get(1,idx=>idx.Index(“newsttitle”);

ElasticSearch已安装并正在运行,但当我尝试运行这些代码行时,它什么也不做。没有创建索引。

有几件事需要考虑

  • client.Index(新闻,idx=>idx.Index(“新闻标题”)不检查索引文档请求的响应

    检查响应是否成功是一个好主意-NEST 对所有具有的响应都具有方便的
    .IsValid
    属性 考虑响应是否有效的语义

  • var response=client.Get(1,idx=>idx.Index(“newsttitle”)
    尝试从id为
    1
    “NewsTitle”
    索引中获取文档,但刚索引的文档没有id,因此Elasticsearch在索引文档时将生成一个随机的id

    NEST有一个约定,如果POCO to索引有一个
    Id
    属性,它将使用该属性作为文档的Id。因此,如果将
    News
    类修改为包含
    int
    Id属性,并且为索引的实例分配了一个Id属性值
    1
    ,get请求将返回索引文档


  • 您正在发出请求,然后解析响应。因此,根据请求参数确定您得到的NewTitle响应的数量。您可能只得到一个索引的一个响应。您可能需要更改请求以获得更多结果。