0

ElasticSearch documentation is far from decent, so I ask here after reading them.

I've put up the easiest elasticsearch example after looking at their horrific documentation.

I got local elasticsearch client (starts on localhost:9200) and NEST library to try and put up a simple console program that indexes some files and try to search them by name.

Can someone help me and tell me why I don't find any result?

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;
        }
    }
}

Thanks

2
  • bool firstRun = false; if (firstRun) { /* when will this run? */ } Commented Jul 11, 2019 at 16:33
  • @JacobKrall Yeah, sorry, I set it true just to run it the first time to create the index (each time i restart elasticsearch server). You're right! Btw the index was already created when I ran query Commented Jul 11, 2019 at 22:18

1 Answer 1

1

Simple Example For You

Model

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="[email protected]"
            };

            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")
                    )
            )
          );

Like Query in Elastic search Example

 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() + "*")
                )))));
Sign up to request clarification or add additional context in comments.

1 Comment

Please kind sir, could you also provide an example to search in "Like"? Ex: string.contains or SQL Like '%%'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.