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))
)
)
)
)
);
据我所知,bool
和must
一起相当于AND
运算符。我有一个包含两个文档的索引,只有一个匹配两个约束(macaddress
和another_field
),但Elasticsearch返回两个。
这是映射:
{
"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": {}
}
}
我还试图制作一个JSON查询,但我得到了0次点击:
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": []
}
}
答案 0 :(得分:1)
term
过滤器不会分析要搜索的文本。这意味着,如果您搜索000A8D810F5A
,这正是搜索的内容(包括大写字母)。
但是,您的macAddr
和insturmentName
字段及其他字段只是string
个。意思是,他们使用standard
分析器来降低术语。所以,你正在搜索000A8D810F5A
,但在索引中你可能有000a8d810f5a
(注意小写字母)。
因此,您需要拥有这些字段not_analyzed
或使用keyword
分析器进行分析,或者,如果您希望默认使用standard
分析器对其进行分析,请添加{{ 1}}应分析.raw
或not_analyzed
的多字段,并在搜索中使用该字段。例如,keyword
。
建议的映射:
"term" : { "macAddr.raw" : "000A8D810F5A" }
建议查询:
"mappings": {
"historyRecord": {
"properties": {
"groupName": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"groupNo": {
"type": "integer"
},
"instrType": {
"type": "integer"
},
"instrumentAddress": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"insturmentName": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"macAddr": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"uhhVersion": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}