0

i have not been able to 'programmatically' set the similarity on a field in elasticsearch using Nest.

here's an example of how i set up my index. it's within the multifield mapping where i'd like to set the similarity so i can experiment with things like BM25 similarity...

(see the props > multifield section below)...

var createInd = client.CreateIndex("myindex", i =>
{
    i
        .Analysis(a => a.Analyzers(an => an
            .Add("nameAnalyzer", nameAnalyzer)
            )
        .AddMapping<SearchData>(m => m
            .MapFromAttributes()
            .Properties(props =>
            {
                props                                           
                    .MultiField(mf => mf
                        //title
                        .Name(s => s.Title)
                        .Fields(f => f
                            .String(s => s.Name(o => o.Title).Analyzer("nameAnalyzer"))
                            .String(s => s.Name(o => o.Title.Suffix("raw")).Index(FieldIndexOption.not_analyzed))
                        )
                    );
                    ...

1 Answer 1

1

It was just recently made possible with this commit to set the similarity on a string field. You can now do this:

.String(s => s.Name(o => o.Title).Similarity("my_similarity")

This is assumming you already have the similarity added to your index. NEST is lacking a bit of flexibility at the moment for actually configuring similarities. Right now you have to use the CustomSimilaritySettings class. For example:

var bm25 = new CustomSimilaritySettings("my_similarity", "BM25");
bm25.SimilarityParameters.Add("k1", "2.0");
bm25.SimilarityParameters.Add("b", "0.75");

var settings = new IndexSettings();
settings.Similarity = new SimilaritySettings();
settings.Similarity.CustomSimilarities.Add(bm25);

client.CreateIndex("myindex", c => c.InitializeUsing(settings));

It would be nice to be able to do this via the fluent API when creating an index. I am considering sending a PR for this before the 1.0RC release.

2
  • thanks greg... also i sure could use your help with what i'm attempting to accomplish. i'm at wits end and can't figure it out. in the end, i don't think my similarity changes are helping. i want to be able to 'sort' my results based on exact matches first then by fuzzy matches. i've got examples and can put together some more complete ones for you somehow if you'll let me. but i've tried so many ways but it's not working. trouble is, if a document contains the text i'm searching more than once, it puts that higher in the sort - which makes little sense for my purposes.
    – bigerock
    Jun 29, 2014 at 18:39
  • @bigerock can you create a gist with your examples? I don't mind taking a look. Jun 30, 2014 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.