繁体   English   中英

有没有办法在 MongoDB(使用 Atlas Search)的搜索索引中包含 Int32 字段?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM