Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Last active October 7, 2015 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yegoroff/3094283 to your computer and use it in GitHub Desktop.
Save Yegoroff/3094283 to your computer and use it in GitHub Desktop.
PlainElastic.Net mapping builder sample
using System;
using System.Collections.Generic;
using System.Linq;
using PlainElastic.Net;
using PlainElastic.Net.Mappings;
using PlainElastic.Net.Queries;
using PlainElastic.Net.Serialization;
using PlainElastic.Net.Utils;
namespace MappingSample
{
class Program
{
class Address
{
public string AddressLine { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
class Contact
{
public string Name { get; set; }
public string Department { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
}
class Company
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Fax { get; set; }
public Address Address { get; set; }
public List<Contact> Contacts { get; set; }
}
static void Main(string[] args)
{
var connection = new ElasticConnection("localhost", 9200);
var serializer = new JsonNetSerializer();
// drop/create test index
try
{
connection.Delete(Commands.Index(index: "store"));
}
// swallow case when no index found
catch (OperationException) { }
connection.Post(Commands.Index(index: "store"));
// Put mapping
string jsonMapping = BuildCompanyMapping();
string result = connection.Put(new PutMappingCommand("store", "company"), jsonMapping);
Console.WriteLine("Mapping: " + jsonMapping);
Console.WriteLine("JSON result: " + result);
Console.WriteLine();
// Add company to index
var company = new Company
{
Id = "1",
Name = "PAPLE",
Description = "Banana",
Fax = "1234567890",
Address = new Address { AddressLine = "some street", City = "XCity", Country = "XCountry", State = "XX" },
Contacts = new List<Contact>{ new Contact
{
Name = "Vasya",
Age = 2,
Department = "Fruits",
Email = "dont@bother.me",
Address = new Address {AddressLine = "some street", City = "XCity", Country = "XCountry", State = "XX"},
}
}
};
string companyJson = serializer.Serialize(company);
connection.Put(Commands.Index("store", "company", id: company.Id).Refresh(),
companyJson);
// Find company with the name "Paple" operated in "XCity" and contact name "Vasya".
string query = new QueryBuilder<Company>()
.Query(q => q
.Nested(n=>n
.Path(cmp=>cmp.Contacts)
.Query(nq=>nq
.Term(t=>t
.FieldOfCollection(cmp=>cmp.Contacts,contact=>contact.Name)
.Value("Vasya".ToLower())
)
)
)
)
.Filter(f => f
.And(and=>and
.Term(t=>t.Field(cmp => cmp.Name).Value("Paple".ToLower()) )
.Term(t=>t.Field(cmp => cmp.Address.City).Value("XCity".ToLower()) )
)
)
.BuildBeautified();
Console.WriteLine("JSON query:");
Console.WriteLine(query);
Console.WriteLine();
string searchResult = connection.Post(Commands.Search("store", "company"), query);
var foundCompany = serializer.ToSearchResult<Company>(searchResult).Documents.First();
Console.WriteLine("JSON result " + searchResult.BeautifyJson());
Console.WriteLine();
Console.WriteLine("Found Company Name " + foundCompany.Name);
Console.WriteLine();
Console.ReadKey();
}
private static string BuildCompanyMapping()
{
return new MapBuilder<Company>()
.RootObject(typeName: "company",
map: r => r
.All(a => a.Enabled(false))
.Dynamic(false)
.Properties(pr => pr
.String(company => company.Name, f => f.Analyzer(DefaultAnalyzers.standard).Boost(2))
.String(company => company.Description, f => f.Analyzer(DefaultAnalyzers.standard))
.String(company => company.Fax, f => f.Analyzer(DefaultAnalyzers.keyword))
.Object(company => company.Address, address => address
.Properties(ap => ap
.String(addr => addr.City)
.String(addr => addr.State)
.String(addr => addr.Country)
)
)
.NestedObject(company => company.Contacts, o => o
.Properties(p => p
.String(contact => contact.Name)
.String(contact => contact.Department)
.String(contact => contact.Email)
// It's unnecessary to specify opt.Type(NumberMappingType.Integer) cause it will be inferred from property type.
// Showed here only for educational purpose.
.Number(contact => contact.Age, opt => opt.Type(NumberMappingType.Integer))
.Object(ct => ct.Address, oa => oa
.Properties(pp => pp
.String(a => a.City)
.String(a => a.State)
.String(a => a.Country)
)
)
)
)
)
)
.BuildBeautified();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment