elasticsearch,nest,C#,Asp.net,elasticsearch,Nest" /> elasticsearch,nest,C#,Asp.net,elasticsearch,Nest" />

C# 使用NEST for ElasticSearch获取空结果

C# 使用NEST for ElasticSearch获取空结果,c#,asp.net,elasticsearch,nest,C#,Asp.net,elasticsearch,Nest,我正在使用ElasticSearch和NEST C#library for.NET迈出我的第一步。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Nest; public partial class _Default : System.We

我正在使用ElasticSearch和NEST C#library for.NET迈出我的第一步。这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nest;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

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

        var post = new Post();
        post.id = 1;
        post.title = "the title";

        var t = client.Index(post);

        var results = client.Search<Post>(s => s.From(0)
            .Size(10)
            .Fields(f => f.id, f => f.title)
            .Query(q => q.Term(f => f.title, "title", Boost: 2.0))
            );

    }
}
public class Post
{
    public int id { get; set; }
    public string title { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
利用鸟巢;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
var设置=新连接设置(新Uri(“http://localhost:9200/"));
setting.SetDefaultIndex(“Post”);
var客户端=新的ElasticClient(设置);
var post=新的post();
post.id=1;
post.title=“标题”;
var t=客户索引(post);
var results=client.Search(s=>s.From(0)
.尺寸(10)
.Fields(f=>f.id,f=>f.title)
.Query(q=>q.Term(f=>f.title,“title”,Boost:2.0))
);
}
}
公营职位
{
公共int id{get;set;}
公共字符串标题{get;set;}
}

我希望从Post1中得到结果,因为它有“title”关键字,但我得到的结果集是空的。我做错了什么?

将你的文章索引化需要很短的时间。如果你插入一个线程。睡眠(1000);在索引和查询之间,会有结果。

问题在于所使用的
术语。这将只匹配索引的确切文本。
术语
查询不可用

如果您正在进行自由文本搜索,请尝试使用
匹配
查询进行良好的通用自由文本搜索。希望通过熟悉文档,开始发现如何构建有趣而强大的查询


好运

已经回答了:虽然这是一个好地方(我一度对此感到困惑),但我认为这可能是他正在使用的查询类型。如果您需要在
索引()之后立即获得结果,请使用
get()
,这是实时的,或者如果您需要从
Search()
调用索引获取结果()传递
新的索引参数{Refresh=true}
但从长远来看,设计应用程序来处理elasticsearch的近实时性会让你收获更多。永远不要依赖线程。睡眠的原因太多。