Note that there are some explanatory texts on larger screens.

plurals
  1. POFacet on nested field with Elasticsearch C# Mpdreamz/NEST client
    primarykey
    data
    text
    <p>How to list a facet based on a property of a nested field using Mpdreamz/NEST Elasticsearch client?</p> <p>I checked the Nest documentation, but it's not clear how to do it.</p> <p>This is the code that I tried:</p> <pre class="lang-cs prettyprint-override"><code>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(Index = FieldIndexOption.analyzed, Type = FieldType.nested)] public List&lt;Genre&gt; Genres { get; set; } public int Year { get; set; } } public class Genre { // public int Id { get; set; } [ElasticProperty(Index = FieldIndexOption.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&lt;Movie&gt;(); // put documents to the index var genres = new List&lt;Genre&gt;(); for (var i = 0; i &lt; 100; i++) genres.Add(new Genre { GenreTitle = string.Format("Genre {0}", i) }); for (var i = 0; i &lt; 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 =&gt; Guid.NewGuid()).Take(10).ToList() }); } // query with facet on nested field property genres.genreTitle var queryResults = client.Search&lt;Movie&gt;(x =&gt; x .From(0) .Size(10) .MatchAll() .FacetTerm(t =&gt; t .OnField(f =&gt; f.Year) .Size(30)) .FacetTerm(t =&gt; t .Size(5) .OnField(f =&gt; f.Genres.Select(f1 =&gt; f1.GenreTitle) ) ) ); var yearFacetItems = queryResults.FacetItems&lt;FacetItem&gt;(p =&gt; p.Year); foreach (var item in yearFacetItems) { var termItem = item as TermItem; Console.WriteLine(string.Format("{0} ({1})", termItem.Term, termItem.Count)); } /* Returns: 1989 (90) 1988 (90) 1986 (90) 1984 (90) 1983 (90) 1981 (90) 1980 (90) 1987 (89) 1982 (89) 1985 (88) and it's fine! */ var genresFacetItems = queryResults.FacetItems&lt;FacetItem&gt;(p =&gt; p.Genres.Select(f =&gt; f.GenreTitle)); foreach (var item in genresFacetItems) { var termItem = item as TermItem; Console.WriteLine(string.Format("{0} ({1})", termItem.Term, termItem.Count)); } /* Return soemthing: genre (842) 98 (47) 51 (30) 24 (29) 46 (28) and it's BAD! I expect the Genre Title to be listed as string, not as some strange integer */ } } } </code></pre> <p>As result of facet I get:</p> <ul> <li>genre (842)</li> <li>98 (47)</li> <li>51 (30)</li> <li>24 (29)</li> <li>46 (28)</li> </ul> <p>While I expect to get something like:</p> <ul> <li>Genre 1 (842) </li> <li>Genre 2 (47) </li> <li>Genre 3 (30) </li> <li>Genre 4 (29) </li> <li>Genre 5 (28)</li> </ul> <p>What do I do wrong? Where to check the right way of using nested fields in Nest and facets on them?</p> <p>Thank you.</p> <p><strong>UPDATE 1:</strong></p> <p>I found it has something to do with tokenizer/analyzer. If genre name is without spaces or dashes - everything works fine. I also tried not-analyzed index attribute </p> <pre class="lang-cs prettyprint-override"><code>[ElasticProperty(Index = FieldIndexOption.not_analyzed)] public string GenreTitle { get; set; } </code></pre> <p>but it didn't help</p> <p><strong>Update 2:</strong> I added fluent index mapping instead of annotations right after previous index delete like:</p> <pre class="lang-cs prettyprint-override"><code>var settings = new IndexSettings(); var typeMapping = new TypeMapping("movies"); var type = new TypeMappingProperty { Type = "string", Index = "not_analyzed", Boost = 2.0 // Many more options available }; typeMapping.Properties = new Dictionary&lt;string, TypeMappingProperty&gt;(); typeMapping.Properties.Add("genres.genreTitle", type); settings.Mappings.Add(typeMapping); client.CreateIndex("default_index", settings); </code></pre> <p>Now not sure what was wrong with annotations. Is there any additional config needed to use annotations for index settings?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload