我正在使用.NET的ElasticSearch和NEST C#库进行我的第一步.这是我的代码:
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; }
}
我期望从Post 1获得结果,因为其中包含“ title”关键字,但结果集为空.我做错了什么?
解决方法:
问题是正在使用术语查询.这将仅与索引的确切文本匹配.术语查询是useful for id type searching.
如果您要进行*文本搜索,请尝试使用匹配查询进行通用的*文本搜索. You can read more about it here on the official docs,并希望通过熟悉文档来发现如何构建有趣且功能强大的查询.
祝好运