0
votes

i got this elasticsearch instance, and im trying to implement something like a "dynamic" searchtemplate.

Case: I got a userinterface where we can adjust the boosting of certain types. ex. "Boost hits in Articles by 10", "Boost hits on the product page by 20"

Today: I can write up the query itself using fluent DSL, which would be generated everytime someone searches.

Future: I would love to have this in a searchtemplate that would just get updated everytime a change is made, thereby just passing in the template name and a query string - instead of having to look up the current boosting values on every request.

My research: So the nest client supports adding searchtemplates to the ES instance, but it seems like it only supports "inline" scripts and i cant seem to find a way to parse my existing query written in "Fluent DSL" to the JSON equivalent.

Fallback idea: Writing the script manually using json \ query DSL.

Any ideas \ solutions out there? :)

1
Have you tried anything? - Dustin Kingen
For now i have just tried to find out what to try, but i have come up short. so no. Nothing more than trying to explore which properties are available for the SearchTemplate type in NEST. - Christer
what version of NEST are you using? What version of Elasticsearch are you targeting? - Russ Cam
Sorry for the late reply here guys - im using the newest version (5.2), altought 5.3 came out a few days ago, so i will be upgrading before going to production. Romoku, got me on the right path, so i will be adding an answer which uses the updated methods and thats not using the fluent dsl. - Christer

1 Answers

0
votes

Version: ElasticSearch\ElasticSearch.NET\NEST v5.2

As pointed out in the comments, there is a serializer on the NEST client, you can also use the NewtonSoft JSON library as pointet out in this answer - but as also stated, to get the exact JSON use the Nest Client serializer, which wraps NewtonSoft JSON with the correct configuration.

Here is an example on the implementation i did using Object initializer syntax, as the one stated in the answer mentioned above uses an old library.

var searchRequest = new SearchRequest()
        {
            Query = query
        };
        var myBytes = _client.Serializer.SerializeToBytes(searchRequest);
        var jsonSearchTemplate = Encoding.UTF8.GetString(myBytes);

As extra information, this is how you add it to your cluster:

//Define your template ID, this is later used when doing the search.
var templateRequest = new PutSearchTemplateDescriptor("my_template");

//Add your parsed json as the inline script
templateRequest.Template(jsonSearchTemplate);

var response = _client.PutSearchTemplate(templateRequest);

How to query using the template: (I also got multiple parameters that needs to be sent in)

var response =
            _client.SearchTemplate<MyIndexObject>(
                e =>
                    e.Index($"myIndex")
                        .Id("my_template")
                        .Params(new Dictionary<string, object>()
                        {
                            {"query_string", "Obamacare"},
                            {"min_should_match", "70%"}
                        }));

Hope this helps anyone else out there struggling with the same thing.