gpt4 book ai didi

mongodb - Elasticsearch按数组字段大小进行过滤

转载 作者:行者123 更新时间:2023-12-02 23:00:59 26 4
gpt4 key购买 nike

我在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"
}'

河流创建的 map :
"CustomerInfo" : {
"properties" : {
"CustomerCompany" : {
"type" : "string"
},
"CustomerEmail" : {
"type" : "string"
},
"SubmissionDate" : {
"type" : "long"
}
}
}

现在,我要查询具有两个以上客户的文档。 (注意:它可以是任何值)。
我可以使用以下查询在MongoDB中获得结果:
db.CustInfo.find({'$where' : 'this.CustomerInfo.length > 2'})

基于 this问题,我尝试了这个elasticsearch查询:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"exists" : {
"field" : "CustomerInfo"
}
},
{"script" : {
"script" : "doc['CustomerInfo'].size() > 2"
}
}
]
}
}
}
}
}

导致此错误:

nested: GroovyScriptExecutionException[ElasticsearchIllegalArgumentException[No field found for [CustomerInfo] in mapping with types []]];



使用 _source而不是 doc可以提供正确的结果,但是速度很慢。
"script" : {
"script" : "_source.CustomerInfo.size() > 2"
}

有没有其他方法可以根据数组字段大小来过滤结果?
我不想创建一个拥有大量客户的新字段。

MongoDB的:3.0.7

flex 搜索:1.4.2

Elasticsearch-River-Plugin:2.0.9

最佳答案

在Lucent中不存在CustomerInfo字段,存在的字段是CustomerInfo.CustomerCompanyCustomerInfo.CustomerEmailCustomerInfo.SubmissionDate。所以你可以使用"script" : "doc['CustomerInfo.CustomerCompany'].size() > 2"

关于mongodb - Elasticsearch按数组字段大小进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34509510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com