I am struggling with Elasticsearch using NEST for C#. Let's assume an index of UserAccounts which looks like
[{
AccountId: 1,
Name: "Test Account",
Email: "test@test.com",
Phone: "01234/5678",
Street: "test Street 1",
Zip: "12345"
},
{
AccountId: 2,
Name: "Test Akkount",
Email: "test@gmail.com",
Phone: "0987/6543"
Street: "test Street 1",
Zip: "54321"
},
{
AccountId: 3,
Name: "Bla Bla",
Email: "qwer@yahoo.com",
Phone: null,
Street: "bla Street 3",
Zip: "45678
},
{
AccountId: 4,
Name: "Foo",
Email: "asdf@msn.com",
Phone: null,
Street: "ghjk Street 9",
Zip: "65487,
}]
now I want get all accounts similar to my query.
string name = "aggount";
string email = "test@gmail.com";
string phone = "0987/6543";
string street = "test Str 1"
string zip = "54321"
But each field has its own criteria.
Field "name" should match over fuzzy logic.
Field "email" should match to 100% but not when its null or empty.
Field "phone" should match to 100% but not when its null or empty.
Field "street" should only match with fuzzy, when "zip" matches to 100%.
I want a list of account with possibilities. If name matches but email not, than there should be a result because of name. Do elastic trim always the provided values?
If it is possible to get a score per field. But this is a nice to have.
My code do not work because when I provide a email and the email is not matching, elastic skip the match over name.
var response = elasticClient.Search<Accounts>(search => search
.Index(INDEX_NAME_ACCOUNT)
.Query(q => q.Bool(b =>
{
if (name != null)
{
b = b.Should(s => s.Match(m => m.Query(name).Field(f => f.Name).Boost(1.5).Fuzziness(Fuzziness.EditDistance(3))));
}
if (street != null && zipCode != null)
{
b = b.Should(s =>
s.Match(m => m.Query(street).Field(f => f.MainAddress.Street).Boost(0.5).Fuzziness(Fuzziness.EditDistance(3))) &&
s.Match(m => m.Query(zipCode).Field(f => f.MainAddress.Zip).Boost(0.7).Fuzziness(Fuzziness.EditDistance(0)))
);
}
if (string.IsNullOrEmpty(name1) && string.IsNullOrEmpty(street))
{
b = b.Should(s => s.MatchNone());
}
b = b.MustNot(s => s.Match(m => m.Query(null).Field(f => f.DeletedTimestamp)));
return b;
}))
.Explain()
.Human()
);
Thank you in advance