[英]Is there a way to include a Int32 field in a search index in MongoDB (with Atlas Search)?
我在 Mongo Atlas DB 中有一個集合,其中有一個搜索索引,包括一些特定的字符串字段。 我想要做的是在這個搜索索引中包含一個 Int32 字段,以便能夠對這個數字以及其他字段進行搜索。 我嘗試將字段(數字)添加為搜索索引中的新字段,類型為編號,但它不起作用。 我想這是因為它將查詢(一個字符串)與一個 Int32 進行比較,但是有沒有辦法讓它工作? 還是我必須復制另一個字段“NumberString”中的“數字”以包含在搜索索引中?
以下是這些文檔之一的示例:
{
“_id” : ObjectId(“010000000000000000000003”),
“Description” : {
“fr-CA” : “Un lot de test”,
“en-CA” : “A test item”
},
“Name” : {
“fr-CA” : “Lot de test”,
“en-CA” : “Test item”
},
“Number” : 345,
“Partners” : [],
[...]
}
指數:
{
“mappings”: {
“dynamic”: false,
“fields”: {
“Description”: {
“fields”: {
“en-CA”: {
“analyzer”: “lucene.english”,
“searchAnalyzer”: “lucene.english”,
“type”: “string”
},
“fr-CA”: {
“analyzer”: “lucene.french”,
“searchAnalyzer”: “lucene.french”,
“type”: “string”
}
},
“type”: “document”
},
“Name”: {
“fields”: {
“en-CA”: {
“analyzer”: “lucene.english”,
“searchAnalyzer”: “lucene.english”,
“type”: “string”
},
“fr-CA”: {
“analyzer”: “lucene.french”,
“searchAnalyzer”: “lucene.french”,
“type”: “string”
}
},
“type”: “document”
},
“Number”:
{
“representation”: “int64”,
“type”: “number”
},
“Partners”: {
“fields”: {
“Name”: {
“type”: “string”
}
},
“type”: “document”
}}}}
最后是我嘗試做的查詢。
db.[myDB].aggregate([{ $search: { "index": "default", "text": { "query": "345", "path": ["Number", "Name.fr-CA", "Description.fr-CA", "Partners.Name"]}}}])
對於此示例,我希望將查詢應用於 Number、Name、Description 和 Partners,並返回匹配的所有內容。 我希望有項目#345,還有名稱或描述中包含 345 的任何項目。 可能嗎?
謝謝 !
使用您當前的數據類型,您應該能夠在文本中搜索#345。 但是,我會像這樣構造查詢,以支持數字字段:
db.[myDB].aggregate([
{
$search: {
"index": "default",
"compound": {
"should":[
{
"text": {
"query": "345",
"path": ["Name.fr-CA", "Description.fr-CA", "Partners.Name"]
}
},
{
"near": {
"origin": 345,
"path": "Number",
"pivot": 2
}
}
]
}
}
}
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.