Elasticsearch Bulk and Search is a high level library to make easy basic query operations (get, search and scroll) and index operations (single index or bulk), besides paging, sorting and query buider.
Sample Entity Class (Person)
public class Person
{
public string Id { get; set; }
public DateTime CreateDate { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public static string GenerateIndexName(string defaultIndex, Person person)
{
var suffix = person.CreateDate.ToString("yyyy-MM");
return $"{defaultIndex}-{suffix}";
}
}
Init commad and query objects
var elasticsearchOptions = new ElasticsearchOptions
{
WriteUrl = "http://localhost:9200",
ReadUrl = "http://localhost:9200",
DefaultTypeName = "docs",
TimeoutInSeconds = 60,
MaximumRetries = 5,
User = "user",
Pass = "pass"
};
IElasticsearchCommand<Person> PersonCommand = new ElasticsearchCommand<Person>(elasticsearchOptions, Person.GenerateIndexName);
IElasticsearchQuery<Person> PersonQuery = new ElasticsearchQuery<Person>(elasticsearchOptions);
Using GenerateIndexName you can customize index per document based on your properties (used to create timed index);
Basic commands
// bulk
var persons = new List<Person>
{
{ new Person { Id = "1", Name = "Thiago Barradas", Age = 27, CreateDate = new DateTime(2019, 01, 01) } },
{ new Person { Id = "8", Name = "Ralph Legal", Age = 29, CreateDate = new DateTime(2018, 12, 01) } },
{ new Person { Id = "9", Name = "James Bond", Age = 30, CreateDate = new DateTime(2018, 12, 10) } },
{ new Person { Id = "10", Name = "John Doe", Age = 54, CreateDate = new DateTime(2018, 11, 01) } },
{ new Person { Id = "11", Name = "Jow Troll Moon Do", Age = 58, CreateDate = new DateTime(2018, 05, 25) } }
};
PersonCommand.Bulk(persons);
// upsert
var otherPerson = new Person { Id = "2", Name = "Rafael Barradas", Age = 25, CreateDate = new DateTime(2018, 12, 01) };
PersonCommand.Upsert(otherPerson);
Basic queries
// get by id
Person personX = PersonQuery.Get("8");
Person personY = PersonQuery.Get(4);
// search
var searchOptions = new SearchOptions
{
Page = 1,
Size = 1,
SortField = "name.keyword",
SortMode = SortMode.ASC
};
var query = Query<Person>.DateRange(i => i.Field("createDate").LessThan("2018-12-01"));
var searchResult = PersonQuery.Search(query, searchOptions);
// scroll
var scrollOptions = new ScrollOptions
{
Scroll = "1m",
Size = 2,
SortField = "name.keyword",
SortMode = BulkAndSearch.Models.SortMode.ASC
};
List<Person> scrollPersons = new List<Person>();
var scrollResult = PersonQuery.Scroll(query, scrollOptions);
while (scrollResult.Items.Any())
{
scrollPersons.AddRange(scrollResult.Items);
scrollResult = PersonQuery.Scroll(query, scrollOptions);
}
When some error occurred, an exception is thrown;
PM> Install-Package Easy.Elasticsearch.BulkAndSearch
Please, refer to CONTRIBUTING
Open a new Issue following our issue template ISSUE_TEMPLATE
See in nuget version history
if you liked this project, please make a contribution and help to keep this and other initiatives, send me some Satochis.
BTC Wallet: 1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX