Facet on nested field with Elasticsearch C# Mpdreamz/NEST client

后端 未结 1 1426
太阳男子
太阳男子 2021-02-02 04:52

How to list a facet based on a property of a nested field using Mpdreamz/NEST Elasticsearch client?

I checked the Nest documentation, but it\'s not clear how to do it.

1条回答
  •  梦谈多话
    2021-02-02 05:23

    Hi Author of NEST here,

    If you use the annotations you need to manually call

    var createIndex = client.CreateIndex("default_index", new IndexSettings { });
    client.Map();
    

    Before the first call to index. Nest wont apply the mapping on each index call since that would incur too much of overhead. Older versions of elasticsearch would just create the index if it doesn't exist and would not need the CreateIndex call.

    Since you want to facet on a nested type you have to pass .Nested("genres") to the facet call.

    The numbers you saw were actually the nested docids :)

    Here's my modified program.cs that works:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Nest;
    
    namespace Demo
    {
      class Program
      {
        public class Movie
        {
          public int Id { get; set; }
          public string Title { get; set; }
          public string Description { get; set; }
          [ElasticProperty(Type=FieldType.nested)]
          public List Genres { get; set; }
          public int Year { get; set; }
        }
    
        public class Genre
        {
          //        public int Id { get; set; }
          [ElasticProperty(Index = FieldIndexOption.not_analyzed)]
          public string GenreTitle { get; set; }
        }
    
        static void Main(string[] args)
        {
          var setting = new ConnectionSettings("localhost", 9200);
          setting.SetDefaultIndex("default_index");
          var client = new ElasticClient(setting);
    
          // delete previous index with documents
          client.DeleteIndex();
    
          var createIndexResult = client.CreateIndex("default_index", new IndexSettings { });
          var mapResult = client.Map();
    
          // put documents to the index
          var genres = new List();
          for (var i = 0; i < 100; i++)
            genres.Add(new Genre { GenreTitle = string.Format("Genre {0}", i) });
          for (var i = 0; i < 1000; i++)
          {
            client.Index(new Movie
            {
              Id = i,
              Description = string.Format("Some movie description {0}", i),
              Title = string.Format("Movie Title {0}", i),
              Year = 1980 + (i % 10),
              Genres = genres.OrderBy(x => Guid.NewGuid()).Take(10).ToList()
            });
          }
    
          // query with facet on nested field property genres.genreTitle
          var queryResults = client.Search(x => x
                  .From(0)
                  .Size(10)
                  .MatchAll()
                  .FacetTerm(t => t
                      .OnField(f => f.Year)
                      .Size(30))
                  .FacetTerm(t => t
                      .Size(5)
                      .OnField(f => f.Genres.Select(f1 => f1.GenreTitle))
                      .Nested("genres")
                  )
          );
    
          var yearFacetItems = queryResults.FacetItems(p => p.Year);
          foreach (var item in yearFacetItems)
          {
            var termItem = item as TermItem;
            Console.WriteLine(string.Format("{0} ({1})", termItem.Term, termItem.Count));
          }
    
          var genresFacetItems = queryResults.FacetItems(p => p.Genres.Select(f => f.GenreTitle));
          foreach (var item in genresFacetItems)
          {
            var termItem = item as TermItem;
            Console.WriteLine(string.Format("{0} ({1})", termItem.Term, termItem.Count));
          }
    
        }
      }
    }
    

    0 讨论(0)
提交回复
热议问题