Mongo Atlas Search: 查找精确文档匹配

huangapple go评论48阅读模式
英文:

Mongo Atlas Search: Find Exact Document Matches

问题

我尝试使用Mongo Atlas Search聚合来查找精确匹配我的查询的文档,查询中包含100 - 200个单词的列表。

开始我的聚合查询

{
  '$search': {
     'index': 'default',
     'text': {
       'query': 'yellow pizza blue ',
       'path': 'word'
      }
   }
}, {...

我的集合

{
  "word": "yellow card"
},
{
  "word": "pizza"
},
{
  "word": "blue"
}

我需要它返回“pizza, blue”,而不是返回“yellow card, pizza, blue”。

英文:

Trying to use Mongo Atlas Search aggregation to find documents that precisely match my query which contains a list of 100 - 200 words.

Start of my aggregation query

{
  '$search': {
     'index': 'default',
     'text': {
       'query': 'yellow pizza blue ',
       'path': 'word'
      }
   }
}, {...

My collection

{
  "word": "yellow card"
},
{
  "word": "pizza"
},
{
  "word": "blue"
}

I need it to return “pizza, blue", instead it returns “yellow card, pizza, blue

答案1

得分: 1

你可能只需要使用$indexOfCP进行简单的子字符串搜索

db.collection.find({
  $expr: {
    $ne: [
      -1,
      {
        $indexOfCP: [
          "yellow pizza blue ",
          "$word"
        ]
      }
    ]
  }
})

如果你的搜索列表是以空格分隔的列表,你可以考虑使用$splitwords字段和你的搜索列表拆分成数组,然后使用$setIsSubset来查找匹配项。

db.collection.aggregate([
  {
    "$addFields": {
      "searchList": "yellow pizza blue investment "
    }
  },
  {
    "$addFields": {
      "searchList": {
        "$split": [
          "$searchList",
          " "
        ]
      },
      tokens: {
        "$split": [
          "$word",
          " "
        ]
      }
    }
  },
  {
    "$match": {
      $expr: {
        "$setIsSubset": [
          "$tokens",
          "$searchList"
        ]
      }
    }
  },
  {
    $unset: [
      "tokens",
      "searchList"
    ]
  }
])

Mongo Playground

英文:

You probably just need a simple substring search using $indexOfCP

db.collection.find({
  $expr: {
    $ne: [
      -1,
      {
        $indexOfCP: [
          "yellow pizza blue ",
          "$word"
        ]
      }
    ]
  }
})

Mongo Playground


If your search list is a space-separated list, you can consider $split the words field and your search list into arrays and perform $setIsSubset to find the matches.

db.collection.aggregate([
  {
    "$addFields": {
      "searchList": "yellow pizza blue investment "
    }
  },
  {
    "$addFields": {
      "searchList": {
        "$split": [
          "$searchList",
          " "
        ]
      },
      tokens: {
        "$split": [
          "$word",
          " "
        ]
      }
    }
  },
  {
    "$match": {
      $expr: {
        "$setIsSubset": [
          "$tokens",
          "$searchList"
        ]
      }
    }
  },
  {
    $unset: [
      "tokens",
      "searchList"
    ]
  }
])

Mongo Playground

huangapple
  • 本文由 发表于 2023年2月18日 08:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定