简体   繁体   English

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

[英]Is there a way to include a Int32 field in a search index in MongoDB (with Atlas Search)?

I have a collection in a Mongo Atlas DB on which I have a search index including some specific string fields.我在 Mongo Atlas DB 中有一个集合,其中有一个搜索索引,包括一些特定的字符串字段。 What I want to do is include a Int32 field in this search index to be able to do a search on this number, along with the other fields.我想要做的是在这个搜索索引中包含一个 Int32 字段,以便能够对这个数字以及其他字段进行搜索。 I tried to add the field (Number) as a new field in the search index, with the type number, but it doesn't work.我尝试将字段(数字)添加为搜索索引中的新字段,类型为编号,但它不起作用。 I guess it's because it compares the query, a string, with an Int32, but is there a way to make it work?我想这是因为它将查询(一个字符串)与一个 Int32 进行比较,但是有没有办法让它工作? Or do I have to copy the "Number" in another field "NumberString" to include in the search index?还是我必须复制另一个字段“NumberString”中的“数字”以包含在搜索索引中?

Here is an example of one of these documents:以下是这些文档之一的示例:

{
  “_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” : [],
[...]
}

The index:指数:

{
“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”
}}}}

And finally the query I try to do.最后是我尝试做的查询。

db.[myDB].aggregate([{ $search: { "index": "default", "text": { "query": "345", "path": ["Number", "Name.fr-CA", "Description.fr-CA", "Partners.Name"]}}}])

For this example, I want the query to be applied on Number, Name, Description and Partners and to return everything that matches.对于此示例,我希望将查询应用于 Number、Name、Description 和 Partners,并返回匹配的所有内容。 I would expect to have the item #345, but also any items with 345 in the name or description.我希望有项目#345,还有名称或描述中包含 345 的任何项目。 Is it possible?可能吗?

Thanks !谢谢 !

With your current datatype you, should be able to search for #345 in text.使用您当前的数据类型,您应该能够在文本中搜索#345。 However, I would structure the query like so, to support the numeric field as well:但是,我会像这样构造查询,以支持数字字段:

  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