EsalsticSearch简单示例不起作用c#

时间:2019-07-11 16:11:17

标签: c# elasticsearch

ElasticSearch文档远非得体,因此我在阅读它们后会在这里询问。

在查看了他们可怕的文档之后,我提出了最简单的elasticsearch示例。

我有本地elasticsearch客户端(从localhost:9200开始)和NEST库,试图建立一个简单的控制台程序来对一些文件建立索引,并尝试按名称搜索它们。

有人可以帮助我,告诉我为什么找不到任何结果吗?

using Nest;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElasticHorror
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri connectionString = new Uri("http://localhost:9200");



            //Client settings, index must be lowercase
            var settings = new ConnectionSettings(connectionString).DefaultIndex("tests");
            settings.PrettyJson();
            settings.ThrowExceptions();

            //Client initialization
            var client = new ElasticClient(settings);

            //Index creation, I use a forced bool for testing in a console program only ;)
            bool firstRun = true;
            if (firstRun)
            {
                foreach(string file in Directory.GetFiles(@"G:\SomeFolderWithFiles", "*.*", SearchOption.AllDirectories))
                {
                    Console.WriteLine($"Indexing document {file}");
                    client.IndexDocument<FileProps>(new FileProps(new FileInfo(file)));
                }
            }


            var searchResponse = client.Search<FileProps>(s => s.Query(
                                            doc => doc.Term(t => t.Name, "Test")))
                                 .Documents;



        }

        internal class FileProps
        {

            public FileProps(FileInfo x)
            {
                CreationTime = x.CreationTime;
                Extension = x.Extension;
                FullName = x.FullName;
                Lenght = x.Length;
                Name = x.Name;
                Directory = x.DirectoryName;
            }
            [Date]
            public DateTime CreationTime { get; private set; }
            public string Extension { get; private set; }
            public string FullName { get; private set; }
            public long Lenght { get; private set; }

            public string Name;
            public string Directory;
        }
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

适合您的简单示例

型号

internal class Person
    {
        public int id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string Mnumber { get; set; }
        public string Email { get; set; }

    }

            var settings = new ConnectionSettings(new Uri("http://localhost:9200"));

            settings.DefaultIndex("bar");

            var client = new ElasticClient(settings);



            var person = new Person
            {
                id = 2,
                firstname = "Martijn123Hitesh",
                lastname = "Martijn123",
                Mnumber="97224261678",
                Email="hitesh@gmail.com"
            };

            var indexedResult = client.Index(person, i => i.Index("bar"));

             var searchResponse = client.Search<Person>(s => s
            .Index("bar")
            .Query(q => q
                    .Match(m => m
                    .Field(f => f.firstname)
                    .Query("Martijn123Hitesh")
                    )
            )
          );

类似于Elastic搜索示例中的查询

 var searchResponseww = client.Search<Person>(s => s
           .Index("bar")
           .Query(q => q
            .Bool(b => b
            .Should(m => m
                .Wildcard(c => c
                .Field("firstname").Value("Martijn123".ToLower() + "*")
                )))));