Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Last active December 18, 2015 05:49
Show Gist options
  • Save Yegoroff/5735870 to your computer and use it in GitHub Desktop.
Save Yegoroff/5735870 to your computer and use it in GitHub Desktop.
ES Mapping and MongoDb river configuration
using System;
using Newtonsoft.Json;
using PlainElastic.Net;
using PlainElastic.Net.Mappings;
using PlainElastic.Net.Utils;
namespace ConsoleApplication21
{
internal class Program
{
private static void Main()
{
var connection = new ElasticConnection("localhost", 9200);
if (!IsIndexExists(indexName: "queryreadyproducts", connection: connection))
{
// Create ES index "queryreadyproducts" without types and mappings.
connection.Put(new IndexCommand(index: "queryreadyproducts"));
}
// Put "product" type mapping to index "queryreadyproducts".
string jsonMapping = BuildEsMap(); // Returns mapping for "product" type.
connection.Put(new PutMappingCommand(index: "queryreadyproducts", type: "product"), jsonMapping); // This will create mapping for index "queryreadyproducts" and type "product"
Console.WriteLine("Index 'queryreadyproducts' with type 'products' created");
// Configure MongoDB river plugin.
// Assuming MongoDB river plugin is installed in your ES server plugins directory.
string riverConfiguration = "{ 'type': 'mongodb'," +
" 'mongodb': { 'db': 'brandviewdata', 'collection': 'queryreadyproducts'}, " + // this should correspond to the MongoDB database and collection names
" 'index': { 'name': 'queryreadyproducts', 'type': 'products' }" + // this is ES index and type we created above, MongoDb changes will be synced there.
"}";
riverConfiguration = riverConfiguration.AltQuote(); // Replace ' to ".
connection.Put(new IndexCommand("_river", "mongodb", "_meta"), riverConfiguration);
Console.WriteLine("MongoDb river configured.");
Console.ReadKey();
}
private static string BuildEsMap()
{
string mapping = new MapBuilder<QueryReadyProduct>().RootObject(
typeName: "product",
map: map => map
.All( a=> a.Enabled(false))
.Dynamic(false)
.Properties(p => p
.String(q => q.Name, opt => opt.Analyzer(DefaultAnalyzers.standard))
.Binary(q => q.CustomerId, opt => opt.Index(IndexState.not_analyzed))
.Binary(q => q.ProductId, opt => opt.Index(IndexState.not_analyzed))
.Binary(q => q.CategoryId, opt => opt.Index(IndexState.not_analyzed))
.Binary(q => q.CustomHierarchyId, opt => opt.NullValue("").Index(IndexState.not_analyzed))
)
).BuildBeautified();
return mapping;
}
private static bool IsIndexExists(string indexName, ElasticConnection connection)
{
try
{
connection.Head(new IndexExistsCommand(indexName));
return true;
}
catch (OperationException ex)
{
if (ex.HttpStatusCode == 404)
return false;
throw;
}
}
}
public class QueryReadyProduct
{
[JsonIgnore]
public Object _id { get; set; }
public Guid CustomerId { get; set; }
public Guid ProductId { get; set; }
public Guid CategoryId { get; set; }
public Guid CustomHierarchyId { get; set; }
public String Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment