使用Nest(Elasticsearch)进行方面搜索

时间:2014-04-13 22:25:17

标签: c# elasticsearch nest

我正在使用NEST(版本0.12)Elasticsearch(版本1.0),我正面临着一个问题。 基本上我期待结果与此类似

18至25(10)

26至35(80)

大于35(10)

但我实际得到的是这个

介于(99)

之间

和(99)

35(99)

26(99)

这是我的代码

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;
            }

        }

    }
}

由于

1 个答案:

答案 0 :(得分:1)

根据您看到的输出,我不认为您的FieldIndexOption.not_analyzed正在应用于AgeBand字段。由于这些方面的结果看起来像分析值。您需要在索引创建过程中应用映射作为索引设置的一部分。请尝试以下索引创建代码:

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

请参阅Nest Documentation on Mapping以了解将映射添加到索引的其他方法。