View on GitHub

Notes

reference notes

Using Relevance-Based Search and Search Indexes

Relevance-based search is a way to surface records from a database based on their relevance to a search term. It allows users to find information that matches their query effectively. Here’s an example of how relevance-based search works:

Relevance-Based Search

In relevance-based search, you specify search terms, and the database returns records that match those terms in a way that prioritizes their relevance.

Database Indexes

Database indexes are data structures used by developers and database administrators to improve the efficiency of frequent database queries. They enhance the speed at which data can be retrieved from the database. Indexes are used to optimize query performance and are essential for making applications responsive and scalable.

Search Indexes

Search indexes, on the other hand, are used to specify how records are referenced for relevance-based search, which is the end-users’ way of querying for information. Search indexes define the rules and configurations for search algorithms.

Here’s an example of how a search index might look:

{
  "analyzer": "lucene.standard",
  "searchAnalyzer": "lucene.standard",
  "mappings": {
    "dynamic": true,
    "fields": {
      "title": {
        "type": "string"
      },
      "plot": {
        "type": "string"
      }
    }
  }
}

In this example:

A search index defines how the application’s search algorithm should function, and it can be customized according to the requirements of the application.

Creating a Search Index with Dynamic Mapping

When you create a search index with dynamic mapping, the index queries all fields, including nested fields. Dynamic field mapping searches all fields for the search term, with equal weight placed on all fields.

Creating a Search Index with Static Field Mapping

In contrast, if you create a search index with static field mapping, you specify which fields to index explicitly. Only queries for the specified fields will return results. Other fields won’t be indexed or searchable.

Using $search and Compound Operators

In MongoDB, you can use the $search aggregation stage to perform relevance-based searches. The $search stage allows you to specify various compound operators to control the behavior of the search. These compound operators include:

Here’s an example of how you can use these compound operators within the $search aggregation stage:

$search: {
  "compound": {
    "must": [
      {
        "text": {
          "query": "field",
          "path": "habitat"
        }
      }
    ],
    "should": [
      {
        "range": {
          "gte": 45,
          "path": "wingspan_cm",
          "score": {
            "constant": {
              "value": 5
            }
          }
        }
      }
    ]
  }
}

These compound operators allow you to filter, prioritize, and fine-tune your search results based on various criteria.

Grouping Search Results by Using Facets

When working with relevance-based search, you can use $searchMeta and $facet to group and analyze search results. $searchMeta provides metadata related to the search, while $facet allows you to define facets for grouping results.

Here’s an example of how to use $searchMeta and $facet to group search results:

$searchMeta: {
  "facet": {
    "operator": {
      "text": {
        "query": ["Northern Cardinal"],
        "path": "common_name"
      }
    },
    "facets": {
      "sightingWeekFacet": {
        "type": "date",
        "path": "sighting",
        "boundaries": [
          ISODate("2022-01-01"),
          ISODate("2022-01-08"),
          ISODate("2022-01-15"),
          ISODate("2022-01-22")
        ],
        "default": "other"
      }
    }
  }
}

In this example:

By using $facet, you can organize search results into meaningful categories or facets, making it easier for users to navigate and refine their search.