NEST - 无法索引对象(c#search)

时间:2015-12-27 15:05:50

标签: c# elasticsearch nest

我抓住了最新的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();
        }

1 个答案:

答案 0 :(得分:0)

我尝试过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; }
    }