NEST-无法为对象编制索引(c#搜索)
本文关键字:索引 搜索 对象 NEST- | 更新日期: 2023-09-27 18:28:14
我获得了NEST的最新NuGet,并尝试实现简单的弹性搜索。我的代码不对对象进行索引。IsValid返回false,但我不知道为什么。结果也是空的。这是我的代码片段。我将感谢你的帮助。
public ActionResult Search()
{
var node = new Uri("http://localhost:49997");
var settings = new ConnectionSettings(
node,
defaultIndex: "my-application"
);
var client = new ElasticClient(settings);
var uporabnik = new uporabnik
{
idUporabnik = 99,
ime = "John",
priimek = "Schwarz"
};
var index = client.Index(uporabnik);
if (!index.IsValid)
{
Log.Error("Error when indexing.");
}
var results = client.Search<uporabnik>(s => s.Query(q => q.Term(p => p.priimek, "Novak")));
return View();
}
我尝试了Console.WriteLine(index.ServerError.Error),但出现了一个错误:对象引用未设置为对象的实例。所以我不知道,什么是错误信息。
uporabnik在下面。
public partial class uporabnik
{
public uporabnik()
{
this.ocena = new HashSet<ocena>();
this.predmet = new HashSet<predmet>();
this.studentpredmet = new HashSet<studentpredmet>();
}
[Required]
public int idUporabnik { get; set; }
public Nullable<int> vpisnaStevilka { get; set; }
[Required]
[RegularExpression(@"^[A-z]*$"]
public string ime { get; set; }
[Required]
[RegularExpression(@"^[A-z]*$"]
public string priimek { get; set; }
[RegularExpression(@"^['w'd-'.]+@(['w'd-]+'.)+['w-]{2,4}$"]
public string email { get; set; }
public string geslo { get; set; }
[Required]
public string mobi { get; set; }
[Required]
[RegularExpression(@"^[MZ]*$"]
public string spol { get; set; }
[RegularExpression(@"^[123]{1}$"]
public Nullable<int> letnikStudija { get; set; }
public System.DateTime datumRegistracije { get; set; }
public System.DateTime zadnjiDostop { get; set; }
public int idVloge { get; set; }
[JsonIgnore]
public virtual ICollection<ocena> ocena { get; set; }
[JsonIgnore]
public virtual ICollection<predmet> predmet { get; set; }
[JsonIgnore]
public virtual ICollection<studentpredmet> studentpredmet { get; set; }
[JsonIgnore]
public virtual vloga vloga { get; set; }
}