How to Pass Raw JSON to RegisterPercolator in ElasticSearch NEST?

608 Views Asked by At

I am trying to create an object-agnostic Percolator microservice in C#. I can create and map an index passed into my method using a JSON object for the mapping, I can even then register a percolator against the index using the standard NEST query format, such as this:

var percolateResponse = client.RegisterPercolator<dynamic>(query
    .Name, p=>p
        .Index(index.ActualName)
        .Query(q=>q
            .Term(t=>t
                .OnField("banana")
                .Value("green"))));

The problem is, I need to be able to pass in a JSON of the query and I've been trying to use the following code:

var percolateResponse = client.RegisterPercolator<dynamic>(query
    .Name, p=>p
        .Index(index.ActualName)
        .Query(q=>q.Raw(query.Context)));

The JSON that I am passing in is:

{"query":
    {"term":
        {"banana":
            {"value": "green"}
        }
    }
}

What is happening though, instead of that registering the specified query as a percolator query, it sets the Query value of the RegisterPercolatorRequest to null and basically cancels out the query if it exists.

I have tried a number of different formats of the JSON, and have not found something that the .Query(q=>q.Raw(query.Context)) liked.

Does ANYONE know how to solve this? I have moved closer and closer to a solution to this, but just never quite gotten past this one issue. It is my last remaining roadblock to creating a percolator proxy class. Any help is appreciated. Thanks.

Just for reference, the error I am getting (deep inside the Response object) is this:

[<indexName>] failed to parse query [<queryName>]]; 
nested: QueryParsingException[[<indexName>] [_na] query malformed, 
no field after start_object

I think I have found the solution to what I am trying to do... the above query didn't work, but through some trial and error, I have found out what DOES work to take in an object-agnostic query and register a percolator:

var percolateResponse = client.RegisterPercolator<dynamic>(query
    .Name, p => p
        .Index(index.ActualName)
        .Query(q => q
            .SimpleQueryString(sqs=>sqs
                .Query(query.Context))));

This creates the Query as a Simple Query String and stores it as such in ElasticSearch, and appears to then work when I try to percolate a document that would match that query!

0

There are 0 best solutions below