使用Nest(Elasticsearch)进行Facet搜索

本文关键字:进行 Facet 搜索 Elasticsearch Nest 使用 | 更新日期: 2023-09-27 17:58:13

我正在使用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;
            }
        }
    }
}

感谢

使用Nest(Elasticsearch)进行Facet搜索

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

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

请参阅关于映射的Nest文档,了解将映射添加到索引的其他方法。