4

when i create a search using facets, i want the facet results to be on the whole phrase, not the individual word. and i want it NOT to be case sensitive - as 'not_analyzed' would do.

for example, if i have a music json object and want to organize the facet results based on genre, i want to see each genre as the whole genre term (rhythm and blues) and not one facet for 'rhythm' and one for 'blues', and i want to be able to search on 'rhythm and blues' and have it match 'Rhythm and Blues' (notice case).

it seems the elasticsearch documentation suggests using a custom analyzer of a tokenizer and lowercase filter.

here's the suggestion from elasticsearch i mentioned: (mid-page) http://www.elasticsearch.org/blog/starts-with-phrase-matching/

I want to be able to say something like (in my POCO in pseudo code):

[ElasticProperty(Analyzer = "tokenizer, lowercase"]
public string Genre { get; set; }
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
bigerock
  • 713
  • 1
  • 9
  • 29

1 Answers1

1

Use the multi field type in your mapping. Doing so will allow you to index the Genre field in two ways- analyzed (using the standard or lowercase analyzer) for conducting searches, and not_analyzed for faceting.

For more advanced mappings like this, the attribute based mapping in NEST won't cut it. You'll have to use the fluent API, for example:

client.CreatIndex("songs", c => c
.AddMapping<Song>(m => m
    .MapFromAttributes()
    .Properties(props => props
        .MultiField(mf => mf
            .Name(s => s.Genre)
            .Fields(f => f
                .String(s => s.Name(o => o.Genre).Analyzer("standard"))
                .String(s => s.Name(o => o.Genre.Suffix("raw")).Index(FieldIndexOption.not_analyzed)))))));

Hope this helps!

Greg Marzouka
  • 3,315
  • 1
  • 20
  • 17
  • 1
    instead of bloating the index with two of the same type of information, is it possible to use IndexAnalyzer and SearchAnalyzer attributes on my class object (the object i use to serialize)? i want to be able to do a case insensitive term facet on the genres as well. – bigerock May 09 '14 at 19:41
  • also - getting an error using the code above ... Could not get field name for multifield mapping – bigerock May 09 '14 at 20:44
  • Sorry- just edited the example to include the Name descriptor method, which was causing the "field name for multifield mapping" error. Not sure I totally understand your question about the analyzer attributes- yes you could, but I don't think that will achieve what you want in this case. – Greg Marzouka May 12 '14 at 03:31
  • thanks greg. to try and clarify: i noticed there are class object attributes called IndexAnalyzer and SearchAnalyzer that you can put on a class.property. i didn't know if those could use two different analyzers (one for indexing and one for searching). or would that achieve the same thing as what you've done here? for example: [ElasticProperty(IndexAnalyzer = "fullTerm", SearchAnalyzer = "caseInsensitive")] public List Genres { get; set; } – bigerock May 12 '14 at 13:25
  • By doing that, you'll be applying a different index analyzer and a different search analyzer on the _same_ field. Generally, you want to index the data the same way you would search on it. So by your example, indexing "Rhythm and Blues" would not yield search results for "rhythm and blues". – Greg Marzouka May 12 '14 at 14:29
  • ok. i think i understand. i'll play around with the variations. thanks again. – bigerock May 12 '14 at 20:08
  • one thing i'm running into is how to index a nested type this way. for example, let's say i have a field called credits which has an id and a name. that gets built as a nested object (to have a name/id pair) in my class. how would i create the multi-field in this case? if it helps, i am parsing a delimited (id~name|id~name) string from the sql db i'm loading it from and then creating a 'person' object with a name and id field within my class object mapping. – bigerock May 13 '14 at 15:42
  • @bigerock Take a look at the NestedObject descriptor on the AddMapping API. You would setup the MultiField the same way but on Properties of the nested object. – Greg Marzouka May 13 '14 at 17:55
  • 2
    greg - where is this documentation? maybe that's what i've been missing. i only see the documentation on the http://nest.azurewebsites.net/ website. – bigerock May 13 '14 at 17:56