简体   繁体   中英

MongoDB Atlas Search - How to filter search score

Is there is any fine way to filter result on the basis of searchScore. For eg, I got 15000 results and I want to filter out result by using $match by keeping results with score greater than 15. Problem is that as the number of results increase $match will respond slow

product.aggregate([
  {
    '$search': {
      'text': {
        'query': 'harry potter', 
        'path': 'title'
      }
    }
  }, {
    '$project': {
      'score': {
        '$meta': 'searchScore'
      }
    }
  }, {
    '$match': {
      'score': {
        '$gt': 15
      }
    }
  }
])

What you need is an index. Indexes are secondary data structures used by databases for faster access to certain data you want it to keep track of.

Usually, when you run a selection query, MongoDB does a "collection scan" where it scans every single document in the collection to find the required data. However, with an index, the database engine keeps track of where the required data is and the number of documents it needs to scan is limited. By default, there's an index on _id since the database needs to ensure that no other index has the same _id for every insert or update. You can create your own index on any field in the collection, examples in the official docs

However, be aware that creating indexes will have an impact on the performance inserts and updates since it needs to create an index on every new document inserted. Moreover, creating indexes will take space since they need to be stored in addition to the data. If you have enough RAM to fit the entire index structure in memory, you'll get the best performance.

Take a look at compound and add a filter clause with range .

product.aggregate([
  {
    '$search': {
      'compound': { 
       'must' : [ {
          'text': { 'query': 'harry potter', 'path': 'title'} 
          }],
       'filter' : {
            'range' : { path: "score", gt: 15 } 
        } 
      }
  }, {
    '$project': {
      'score': {
        '$meta': 'searchScore'
      }
    }
  }
  }
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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