0
votes

I am using NEST (ver 0.12) Elasticsearch (ver 1.0) and I'm facing a problem with the facets. Basically I'm expecting the results to be something similar to this

Between 18 and 25 (10)

Between 26 and 35 (80)

Greater then 35 (10)

But what I'm actually getting is this

between (99)

and (99)

35 (99)

26 (99)

This is my code

namespace ElasticSerachTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var setting = new ConnectionSettings(new Uri("http://localhost:9200/"));
            setting.SetDefaultIndex("customertest");
            var client = new ElasticClient(setting);

            var createIndexResult = client.CreateIndex("customertest", new IndexSettings
            {

            });

            // put documents to the index using bulk
            var customers = new List<BulkParameters<Customer>>();
            for (int i = 1; i < 100; i++)
            {
                customers.Add(new BulkParameters<Customer>(new Customer
                {
                    Id = i,
                    AgeBand = GetAgeBand(),
                    Name = string.Format("Customer {0}", i)
                }));
            }


            var bp = new SimpleBulkParameters()
            {
                Replication = Replication.Async,
                Refresh = true
            };

            //first delete
            client.DeleteMany(customers, bp);
            //now bulk insert
            client.IndexMany(customers, bp);


            // query with facet on nested field property genres.genreTitle
            var queryResults = client.Search<Customer>(x => x
                    .From(0)
                    .Size(10)
                    .MatchAll()
                    .FacetTerm(t => t
                        .OnField(f => f.AgeBand)
                        .Size(30))
            );

            var yearFacetItems = queryResults.FacetItems<FacetItem>(p => p.AgeBand);
            foreach (var item in yearFacetItems)
            {
                var termItem = item as TermItem;
                Console.WriteLine(string.Format("{0} ({1})", termItem.Term, termItem.Count));
            }

            Console.ReadLine();
        }

        public static string GetAgeBand()
        {
            Random rnd = new Random();
            var age = rnd.Next(18, 50);

            if (Between(age, 18, 25))
            {
                return "Between 18 and 25";
            }
            else if (Between(age, 26, 35))
            {
                return "Between 26 and 35";
            }

            return "Greater then 35";
        }

        public static bool Between(int num, int lower, int upper)
        {
            return lower <= num && num <= upper;
        }

        [ElasticType(Name = "Customer", IdProperty = "id")]
        public class Customer
        {
            public int Id
            {
                get;
                set;
            }

            public string Name
            {
                get;
                set;
            }

            [ElasticProperty(Index = FieldIndexOption.not_analyzed)]
            public string AgeBand
            {
                get;
                set;
            }

        }

    }
}

Thanks

1
Please be careful with using NEST 0.12 with Elasticsearch 1.0 since it introduced a lot of breaking changes NEST 0.12 can not accomodate please use the 1.0 beta elasticsearch.org/blog/…Martijn Laarman

1 Answers

1
votes

Based on the output you are seeing, I do not think your FieldIndexOption.not_analyzed is being applied to the AgeBand field. As those facet results look like like analyzed values. You need to apply the mapping during the index creating process as part of your index settings. Please try the following index creation code:

 var createIndexResult = client.CreateIndex("customertest", s => s
      .AddMapping<Customer>(m => m
           .MapFromAttributes()
      )
 );

Please see the Nest Documentation on Mapping for some other ways to add the mapping to your index as well.