我在 MongoDB 中有一个具有以下结构的集合:
[
{
"Name" : "A",
"CustomerInfo" : [
{
"CustomerCompany" : "abc",
"CustomerEmail" : "user1@abc.com",
"SubmissionDate" : 1415070090
},
{
"CustomerCompany" : "xyz",
"CustomerEmail" : "user2@xyz.com",
"SubmissionDate" : 1438820000
}
]
},
{
"Name" : "B",
},
{
"Name" : "C",
"CustomerInfo" : [
{
"CustomerCompany" : "mno",
"CustomerEmail" : "user3@mno.com",
"SubmissionDate" : 1412590000
}
]
}
...
]
我使用Elasticsearch-river-plugin在 elasticsearch 中为这个集合创建了一条河流
curl -XPUT 'http://localhost:9200/_river/product_custinfo/_meta' -d '{
"index": {
"name": "CustomerInfo",
"type": "CustomerInfo"
},
"mongodb": {
"db": "MyDB",
"collection": "CustInfo"
},
"type": "mongodb"
}'
河流创建的映射:
"CustomerInfo" : {
"properties" : {
"CustomerCompany" : {
"type" : "string"
},
"CustomerEmail" : {
"type" : "string"
},
"SubmissionDate" : {
"type" : "long"
}
}
}
现在我想查询超过 2 个客户的文档。(注意:它可以是任何值)。我能够使用以下查询在 MongoDB 中获得结果:
db.CustInfo.find({'$where' : 'this.CustomerInfo.length > 2'})
基于这个问题,我尝试了这个弹性搜索查询:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"exists" : {
"field" : "CustomerInfo"
}
},
{"script" : {
"script" : "doc['CustomerInfo'].size() > 2"
}
}
]
}
}
}
}
}
这导致了这个错误:
嵌套:GroovyScriptExecutionException[ElasticsearchIllegalArgumentException[No field found for [CustomerInfo] in mapping with types []]];
使用_source
而不是doc
给出正确的结果,但速度很慢。
"script" : {
"script" : "_source.CustomerInfo.size() > 2"
}
是否有另一种方法可以根据数组字段大小过滤结果?我不想创建一个包含大量客户的新字段。
MongoDB:3.0.7
弹性搜索:1.4.2
弹性搜索河流插件:2.0.9