0

What I would like to do is to store multiple languages under one index field. I understand I need structure as explained here.

Here is sample class with multi-filed property "Name":

public class Data
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Index mapping:

ElasticsearchConfiguration.Instance.Client.CreateIndex("data", i =>
                            i.Settings(s =>
                                s.NumberOfShards(2)
                                .NumberOfReplicas(0))
                            .Mappings(m =>
                                m.Map<Data>(map =>
                                map.AutoMap()
                                .Properties(ps => ps
                                    .Text(s => s
                                        .Name(n => n.Name)
                                        .Fields(f => f
                                            .Text(st => st
                                                .Name("en")
                                                .Analyzer("english"))
                                            .Text(st => st
                                                .Name("de")
                                                .Analyzer("german"))))))));

Mapping "object type -> index name" is done on the creation of the ElasticClient. This is how I send data to the elasticsearch server:

var dataPartitions = DBUtil.GetData().Partition(1000);
foreach (var partition in dataPartitions)
{
    var result = ElasticsearchConfiguration.Instance.Client.Bulk(b => b.IndexMany(partition));
    if (!result.IsValid)
    {
        Environment.Exit(1);
    }
}

So, I have configured index as I wanted, but I don't know how to include separate values for German and English sub properties while indices are creating. Do I must have nested class instead "Name" with hard-coded properties for each language (in this case "en", and "de")? Or, maybe send separate request for the language properties?

2
  • Sorry are you trying to apply multiple analyser on the same value (One name for multiple languages) or have multiple values that need to be analyzed with the right analyzer (One german Name, One English name... that can have)? Commented Nov 25, 2016 at 16:25
  • Second option. I know I can do it if I have properties in Data class like Name_EN, Name_DE. What I'm trying is to avoid update of the Data class after adding a new language. If nothing I will create separate index for each language, but then I have issue with potentially duplicated results (one en, one de) Commented Nov 25, 2016 at 16:49

1 Answer 1

1

Here something that might help.

class Program
    {
        static void Main(string[] args)
        {
            var url = new Uri("http://localhost.fiddler:9200");
            ElasticClient db = new ElasticClient(url);
            string[] lang = { "EN", "DE" };

            db.Map<A>(des => des.AutoMap()
            .Index("a")
            .Properties(
                p => p.Object<JObject>(
                    f => f.Name(n => n.Name).Properties(
                        props => props.String(
                            fen => fen.Name(lang[0])).String(fde => fde.Name(lang[1]))))));

            foreach (var item in Enumerable.Range(0, 10).Select(i => new A
            {
                PropA = i,
                Name = new JObject
                {
                    [lang[0]] = "ABC" + i,
                    [lang[1]] = "GABC"
                }
            }))
            {
                var a = db.Index<A>(item, i => i.Index("a"));
            }


            var items = db.Search<A>(s=>s.Query(q=>q.Match(m=>m.Field("name.EN").Query("ABC1"))));

            Console.ReadLine();
        }
    }

    class A
    {
        [Number]
        public int PropA { get; set; }

        public JObject Name { get; set; }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Nice suggestion, thank you. So, I must have those per-language properties, either as static or dynamic.
@drazen only if you wan't custom analyzers to work on the field. you can add a custom template for auto generation

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.