Reputation: 2282
I am trying to do a simple search in Elasticsearch.
in marvel, I am getting the results back using the following query:
GET /gl_search/poc/_search
{
"query": {
"term": {
"Account_No": "056109"
}
}
}
The type is poc and index is gl_search. See below
{
"took": 28,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 23586,
"max_score": 4.7722025,
"hits": [
{
"_index": "gl_search",
"_type": "poc",
"_id": "AU7fza_MU2-26YcvKIeM",
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(
node,
defaultIndex: "gl_search"
);
When using NEST, I am not getting any results back. I tried the following:
var r = client.Search<poc>(s => s
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Account_No, "056109")
)
and
var r = client.Search<poc>(query => query.Index("gl_search")
.Type("poc")
.From(0)
.Size(100)
.Filter(x => x.Term(p=> p.Journal_ID, "056109")));
Settings are:
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(
node,
defaultIndex: "gl_search"
);
Update
I dont see anything in Fiddler. I assume I would see something there. If so, is it the way I set it up or something kind of virus protection blocking it
Upvotes: 0
Views: 1412
Reputation: 125538
As pointed out in the comments, NEST by default will camelcase .NET object property names when serializing the SearchDescriptor<T>
into the query DSL. So, in your example above,
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var connection = new InMemoryConnection(settings);
var client = new ElasticClient(connection: connection);
var r = client.Search<poc>(s => s
.Index("gl_search")
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Account_No, "056109")
)
);
Console.WriteLine(Encoding.UTF8.GetString(r.RequestInformation.Request));
}
produces the following query DSL (notice the property is account_No
)
{
"from": 0,
"size": 10,
"query": {
"term": {
"account_No": {
"value": "056109"
}
}
}
}
Now, there are two real options for addressing this
1.Use a string for the property name, bypassing NEST's camelcasing convention but at the expense of not working with lambda expressions and all of the type safety that they provide
var r = client.Search<poc>(s => s
.Index("gl_search")
.From(0)
.Size(10)
.Query(q => q
.Term("Account_No", "056109")
)
);
2.Change the default serialization options for NEST; useful if all of you properties are cased as per your examples
// change the default camelcase property name serialization
// behaviour with .SetDefaultPropertyNameInferrer
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.SetDefaultPropertyNameInferrer(s => s);
var client = new ElasticClient(settings);
var r = client.Search<poc>(s => s
.Index("gl_search")
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Account_No, "056109")
)
);
A Func<string, string>
is passed to .SetDefaultPropertyNameInferrer()
that will be called for each property that will be serialized to form the json query DSL that is sent to Elasticsearch.
Upvotes: 1