Reputation: 4012
I have an index with this mapping:
{
"SampleIndex" : {
"mappings" : {
"properties" : {
"$type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"data" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "keyword"
},
"isActive" : {
"type" : "boolean"
},
"isDeleted" : {
"type" : "boolean"
},
"ownerSSN" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"ownerId" : {
"type" : "keyword"
},
"type" : {
"type" : "integer"
}
}
}
}
now I want to create this query:
select * From SampleIndex where ownerSSN = "someSNN" and ownerId = "someOwnerId" and type = 1
I try to produce my query by this code using NEST:
var result = await ElasticClient.SearchAsync<BankAccountReadModel>(
s => s
.Index(IndexName)
.Query(q =>
q.ByOwnerId(personageId) &&
q.ByOwnerSSN(nationalCode) &&
q.ByType(type)
));
return result.Hits.Select(x => x.Source).ToList();
and my Extension methods are listed below:
internal static class BankAccountFilterExtention {
public static QueryContainer ByOwnerId(
this QueryContainerDescriptor<BankAccountReadModel> search
, Guid? id) {
if (id.HasValue)
return search.Term(x =>
x.PersonageId, id.Value.ToString());
return null;
}
public static QueryContainer ByOwnerSSN(
this QueryContainerDescriptor<BankAccountReadModel> search
, string nationalId)
{
if (!string.IsNullOrEmpty(nationalId))
return search.Term(x =>
x.OwnerNationalCode,nationalId
);
return null;
}
public static QueryContainer ByType(
this QueryContainerDescriptor<BankAccountReadModel> search,
BankAccountType? type)
{
if (type.HasValue)
{
search.Term(b=> b.Type,type.Value);
}
return null;
}
}
I expect that when I run this query by "SomeSSN" and type = 1 this retrieves only the records which match both of these values. but I am getting each record those have "someSSN" or type =1.
how can I correct my query?
Upvotes: 0
Views: 501
Reputation: 217274
In ByType
you're not returning the term query
public static QueryContainer ByType(
this QueryContainerDescriptor<BankAccountReadModel> search,
BankAccountType? type)
{
if (type.HasValue)
{
return search.Term(b=> b.Type,type.Value); <--- change this
}
return null;
}
Upvotes: 2