Reputation: 1
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
Upvotes: 0
Views: 192
Reputation: 1549
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() + "*")
)))));
Upvotes: 1