Asp.Net Core 中使用Nest:6.5.1框架查询ElasticSearch数据,使用小结。

模型类:

public class Computer
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public string OtherInfo { get; set;}
    }

插入数据:

数据太多,插入时可能有些不成功。

索引要全为小写

var nodes = "http://192.168.50.233:9200/".Split(';').Select(t => new Uri(t));
            var pool = new StaticConnectionPool(nodes);
            var connectionSettings = new ConnectionSettings(pool);
            var es = new ElasticClient(connectionSettings);

            Computer cp1 = new Computer { Id = 5, Name = "神州笔记本1", OtherInfo = "5" };
            Computer cp2 = new Computer { Id = 2, Name = "联想笔记本1", OtherInfo = "2" };
            Computer cp3 = new Computer { Id = 3, Name = "戴尔笔记本1", OtherInfo = "3" };
            Computer cp4 = new Computer { Id = 4, Name = "惠普笔记本1", OtherInfo = "4" };
            List<Computer> cps = new List<Computer> { cp1, cp2, cp3, cp4 };

            //http://192.168.50.233:9200/realyuseit/computer/_search?typed_keys=true
            Result result = Result.NotFound;
            int n = 0;
            cps.ForEach(cp =>
            {
                result = es.Index(cp, t =>
                               t.Index("realyuseit")
                               .Type(TypeName.Create<Computer>())
                               .Id(cp.Id)
                ).Result;
                if (result == Result.Created || result == Result.Updated) n++;
            });
            Console.WriteLine(n);

查询:

var nodes = "http://192.168.50.233:9200/".Split(';').Select(t => new Uri(t));
            var pool = new StaticConnectionPool(nodes);
            var connectionSettings = new ConnectionSettings(pool);
            var es = new ElasticClient(connectionSettings);
            bool eb = es.IndexExists("realyuseit").Exists;
            var result = es.Search<Computer>(x =>
                                    x.Index("realyuseit")
                                    //.Query(q => q.Match(m => m.Field(f => f.Name).Query("其他笔记本6"))) //单字段全文关键字检索 只要Name中包含值即可,且自带分词 4条数据符合
                                    //.Query(q => q.MultiMatch(m => m.Fields(fd=>fd.Fields(f=>f.Name,f=>f.OtherInfo)).Query("1神23456789"))) //多字段全文关键字检索 Name或OtherInfo包含该值即可,且自带分词 4条数据符合
                                    //.Analyzer("") // 该分词方法可不需要,因为上面的查询自带分词
                                    //.Query(q => q.Bool(b=>b.Must(m=>m.Term(p=>p.Field(f=>f.Id).Value(4)))))  //条件必须符合,无分词,有一些数据类型可能查询失败
                                    //.Query(q => q.Range(c => c.Field(f => f.Id).LessThanOrEquals(5).GreaterThanOrEquals(3))) //范围查询
                                    .Sort(t => t.Ascending(p=>p.Id)) //id升序
                                    //.From(0) //分页
                                    //.Size(3)
            );
            //以第二次为准,es的特点大家也都知道,我一个小菜鸟还不知道怎么解决,不过查2次够我用了
            if (result.Documents.Count == 0)
            {
                result = es.Search<Computer>(x =>
                                    x.Index("realyuseit")
                                    //.Query(q => q.Match(m => m.Field(f=>f.Name).Query("其他笔记5本6"))) //单字段全文关键字检索 只要Name中包含该值即可,且自带分词
                                    //.Query(q => q.MultiMatch(m => m.Fields(fd => fd.Fields(f => f.Name, f => f.OtherInfo)).Query("1神23456789"))) //多字段全文关键字检索 Name或OtherInfo包含该值即可,且自带分词
                                    //.Analyzer("")  // 该分词方法可不需要,因为上面的查询自带分词
                                    //.Query(q => q.Bool(b => b.Must(m => m.Term(p => p.Field(f => f.Id).Value(4))))) //条件必须符合的查询,无分词,有一些数据类型可能查询失败
                                    //.Query(q=>q.Range(c=>c.Field(f=>f.Id).LessThanOrEquals(5).GreaterThanOrEquals(3))) //范围查询
                                    .Sort(t => t.Ascending(p => p.Id))
                                    //.From(0) //分页
                                    //.Size(3)
                         );

                //多个条件一起搜索,例子如下
                //var matchQuery = new List<Func<QueryContainerDescriptor<Computer>, QueryContainer>>
                //{
                //    must => must.Bool(b => b.Must(m => m.Term(p => p.Field(f => f.Id).Value(5)),
                //                                    m => m.Term(p => p.Field(f => f.Name).Value("神州笔记本1"))
                //                                 )
                //                     ),
                //    range => range.Range(c => c.Field(p => p.Id).LessThanOrEquals(5).GreaterThanOrEquals(3))
                //};
                //var tr = es.Search<Computer>(x=>x.Index("realyuseit").Query(q=>q.Bool(b=>b.Must(matchQuery))));

            }
            var res = result.Documents.ToList();
            var rb = true;

删除索引及删除数据,还有点问题没有解决,暂时不发。

 

 

 

上一篇:Eloquent JavaScript #15# Asynchronous Programming(渣翻)


下一篇:[Nest] 02.nest之控制器