模糊文本搜索 MongoDb Atlas 的搜索索引不适用于字符串数组

时间:2021-04-07 11:00:31

标签: mongodb mongodb-atlas fuzzy-search mongodb-indexes

我有以下架构

collectionName:{
  name:string,
  address:string,
  info:["info1","info2",....."infon"]  // this is array of strings
}

我在mongodb Atlas中创建了搜索索引如下

  {
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": [
        {
          "multi": {
            "simpleAnalyzer": {
              "analyzer": "lucene.simple",
              "type": "string"
            }
          },
          "type": "string"
        }
      ],
       "address": [
         {
          "multi": {
            "simpleAnalyzer": {
              "analyzer": "lucene.simple",
              "type": "string"
            }
          },
          "type": "string"
        }
       ],
       "info": [
        {
          "type": "string"
        }
      ]
    }
  }
}

以下查询适用于 address 和 name ,但不适用于 info 。

db.collectionName.aggregate([
    {
        $search: {
            index:'txtIdx', //this is my index name
            text: {
                query: "info1",
                path: "info",
               // path:['name','address','info'],
                fuzzy: {
                  maxEdits: 2,
                  prefixLength: 3,
                },
            },
        },
    },
    {
        $project: {
          _id: 0,
          name: 1,
          address:1,
          info:1,
          score: { $meta: "searchScore" }
    }
    },
    {$limit:100},
    {
        $sort:{
            score:-1
        }
    }
])

我什至尝试为 info 字段创建索引,如下所示

  "info":{
    {
      "type": "document"
    },
    {
      "type": "string"
    }
   }

但即使字符串存在于信息数组中,它仍然给出空结果 有人能告诉我我哪里出错了吗?此外,我只想使用文本搜索索引方式来执行此操作...所以请告诉我如何解决此问题?

0 个答案:

没有答案
相关问题