Help needed On Nest using OIS

(Joey Lu) #1

So I am doing a keyword searching using Asp.Net and Elastic. This is my code (Dynamic Query):

public List<string> SearchFiles(string[] searchPattern, string indexName, string docType, DateTime date)
      IEnumerable<string> searchPartterns = searchPattern.AsEnumerable();
      var response = client.Search<ElasticSearchFile>(new SearchRequest<ElasticSearchFile>
       Query = new QueryContainer(new BoolQuery
                   Must = BuildPhraseQueryContainer(searchPattern, docType),
                Should = new List<QueryContainer>{  new QueryContainer(new DateRangeQuery
                     Name = "date",
                         Field = "date",
                   GreaterThanOrEqualTo = date.ToString(DateFormat.year_month_day)
                 var files = response.Documents;
               var result = (from f in files
                           select f.filename).ToList();
               return result;
           private List<QueryContainer> BuildPhraseQueryContainer(string[] terms, string docType)
               List<QueryContainer> _result = new List<QueryContainer>();
            QueryContainer line1 = new QueryContainer(new TermQuery
                   Field = new Field("filetype"),
                      Value = docType.ToLower()
                foreach (string term in terms)
                   QueryContainer result = new QueryContainer(new TermQuery
                        Field = new Field("_file_content"),
                         Value = term.ToLower()
                  return _result;

config is a global ConnectionSettings and client is a global ElasticClient(config).

It worked fine yesterday and without any code change, today it stopped working. I got the debug info, it says it is a bad request. Here is the request string captured:

GET antra/elasticsearchfile/_search?

Here is the error message:

Invalid NEST response built from a unsuccessful low level call on POST: /antra/elasticsearchfile/search?typed_keys=true
# Audit trail of this API call:
_ - [1] BadResponse: Node: http://localhost:9200/ Took: 00:00:00.0580006

# OriginalException: System.Net.WebException: The remote server returned an error: (400) Bad Request.
_ at System.Net.HttpWebRequest.GetResponse()_
_ at Elasticsearch.Net.HttpConnection.Request[TResponse](RequestData requestData) in c:\Projects\elastic\net-master\src\Elasticsearch.Net\Connection\HttpConnection.cs:line 167

I kinda know where the problem is but I do not know how to solve it. Here are my questions: 1. Why is it generating a POST (in error message) even I am trying to search? 2. How could I remove the 2nd outtest {} (around "query") to make my request valid?

I am using Elastic Search 5.5 and Nest 6.0


(Russ Cam) #2

Nest 6.0 is not compatible with Elasticsearch 5.5. Please use the latest Nest 5.x, which at time of writing is 5.6.0 and let me know if your issue is resolved.

(Joey Lu) #3

Thanks for the reply.
I downgraded my NEST to 5.6.
The problem still exists.
However, when I remove my SHOULD, it worked. Looks like my Should query is having some issue. Is it the way of setting up kind of a cutting off time on search? If not (well, it has to be not since mine is not working), how should I write the Should part?

(Joey Lu) #4

All right. I tried it out.
Apparently the time format is the issue. I changed it to

QueryContainer lastLine = new QueryContainer(new DateRangeQuery
                Name = "date",
                Field = "date",
                GreaterThanOrEqualTo = date.ToString(DateFormat.epoch_millis)

and it worked.

Thanks again.
Well though, I think NEST could have a better documentation, especially on the OIS format.

(Russ Cam) #5

Glad you got it sorted.

With regards to "format", the parameter is used to tell Elasticsearch how to parse the date string in JSON into a Date object representation on the server side for Elasticsearch to work with.

By default, the client always sends DateTime and DateTimeOffset in ISO8601 format, using the default date serialization within Json.NET, and ISO8601 format can be parsed by Elasticsearch using the default "format", so it doesn't need to be specified. If you need to send any other format however, you would need to do two things

  1. tell Elasticsearch how it can parse a date from the string using the "format" parameter on the request

  2. serialize the operator values of the query using that format. The [DateRangeQuery] ( accepts DateMath for the operator values (GreaterThan, etc.) and there are implicit conversions from string and DateTime to DateMath, but with the latter, you cannot control the serialization of DateTime inside of DateMath. So in order to work with a "format", you need to pass a string for DateMath as you have done.

Always keen to hear feedback on the documentation. Which particular areas do you feel we are missing?

(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.