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();
config.DefaultIndex(indexName);
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()
});
_result.Add(line1);
foreach (string term in terms)
{
QueryContainer result = new QueryContainer(new TermQuery
{
Field = new Field("_file_content"),
Value = term.ToLower()
});
_result.Add(result);
}
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?
{
{"query":
{"bool":
{"must":
[
{"term":
{"filetype":{"value":"resume"}}
},
{"term":
{"_file_content":{"value":"java"}}
}
],
"should":[
{"range":
{"date":{"gte":"90ear_0onA12_1a90","_name":"date"}}
}
]
}
}
}
}
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
Thanks