var res = esclient.Search<MyClass>(q => q
.Query(fq => fq
.Filtered(fqq => fqq
.Query(qq => qq.MatchAll())
.Filter(ff => ff
.Bool(b => b
.Must(m1 => m1.Term("macaddress", "mac"))
.Must(m2 => m2.Term("another_field", 123))
)
)
)
)
);
As far as I can understand the bool and must together are the equivalent of the AND operator. I've got an index with two documents in it, only one matches the two constraints (macaddress and another_field) but Elasticsearch returns both.
this is the mapping:
{
"reviewer-test-index": {
"aliases": {},
"mappings": {
"historyRecord": {
"properties": {
"groupName": {
"type": "string"
},
"groupNo": {
"type": "integer"
},
"instrType": {
"type": "integer"
},
"instrumentAddress": {
"type": "string"
},
"insturmentName": {
"type": "string"
},
"macAddr": {
"type": "string"
},
"uhhVersion": {
"type": "string"
}
}
},
"settings": {
"index": {
"creation_date": "1434557536720",
"number_of_shards": "1",
"number_of_replicas": "0",
"version": {
"created": "1050299"
},
"uuid": "FfQADLGVQVOPV3913exKsw"
}
},
"warmers": {}
}
}
I also tried to make a JSON query but I get 0 hits:
GET _search
{
"query" :{
"filtered": {
"query": {
"match_all": { }
},
"filter": {
"bool" : {
"must" : [
{"term" : { "macAddr" : "000A8D810F5A" } },
{"term" : { "insturmentName" : "Amin's furnace" } },
{"term" : { "instrumentAddress" : "8D810F5A"}},
{"term" : { "uhhVersion" : "v2.5"}},
{"term" : { "groupName" : "Amin's Group"}},
{"term" : { "groupNo" : 2}},
{"term" : { "instrType" : 60}}
]
}
}
}
}
}
Response:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 4,
"successful": 3,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
termfilters don't analyze the text to be searched. Meaning, if you search for000A8D810F5A, this is exactly what is searching for (upper-case letters included).But, your
macAddrandinsturmentNamefields and others are juststrings. Meaning, they use thestandardanalyzer which lowercases the terms. So, you are searching for000A8D810F5Abut in the index you probably have000a8d810f5a(notice the lowercase letters).So, you need to either have those fields
not_analyzedor analyzed with thekeywordanalyzer or, if you want to keep them analyzed by default withstandardanalyzer, add a.rawmultifield that should benot_analyzedorkeywordanalyzed and use that in your search. For example,"term" : { "macAddr.raw" : "000A8D810F5A" }.Suggested mapping:
Suggested query: