索引

索引可以与 SQL 中的表或 MongoDB 中的集合相媲美。索引由 uid 定义,并包含以下信息:

  • 主键
  • 可以根据需要配置的默认设置: 相关性规则、同义词、停止词和字段属性。

假设您管理一个包含有关电影信息的数据库。您可能希望保留多种类型的文档,比如电影、电视节目、演员、导演等等。这些类别中的每一个都将用 Meilisearch 的一个索引来表示。每个索引都保存有关文档中找到的字段的信息,包括 Meilisearch 如何处理这些字段以及它们的重要性顺序。另外,每个词都有自己的同义词、相关规则和停止词。一个索引的设置不会影响其他索引。例如,它意味着您可以在同一个服务器上创建movie索引的同义词和custumes索引的不同同义词。

索引的创建

我们将创建一个名为“movie”的索引。下面的代码将创建movie索引并向其中添加一个示例文档。

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/movies/documents' \
  -H 'Content-Type: application/json' \
  --data-binary '[{
      "id": 287947,
      "title": "Shazam",
      "poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
      "overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
      "release_date": "2019-03-23"
  }]'
# JS
client.index('movies').addDocuments([{
    id: 287947,
    title: 'Shazam',
    poster: 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
    overview: 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
    release_date: '2019-03-23'
}])
# python
client.index('movies').add_documents([{
  'id': 287947,
  'title': 'Shazam',
  'poster': 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
  'overview': 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
  'release_date': '2019-03-23'
}])
# php
$client->index('movies')->addDocuments([
  [
    'id' => 287947,
    'title' => 'Shazam',
    'poster' => 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
    'overview' => 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
    'release_date' => '2019-03-23'
  ]
]);
# java
client.index("movies").addDocuments("[{"
  + "\"id\": 287947,"
  + "\"title\": \"Shazam\","
  + "\"poster\": \"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\","
  + "\"overview\": \"A boy is given the ability to become an adult superhero in times of need with a single magic word.\","
  + "\"release_date\": \"2019-03-23\""
  + "}]"
);
# ruby
client.index('movies').add_documents([
  {
    id: 287947,
    title: 'Shazam',
    poster: 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
    overview: 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
    release_date: '2019-03-23'
  }
])
# go
documents := []map[string]interface{}{
  {
    "id":           287947,
    "title":        "Shazam",
    "poster":       "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
    "overview":     "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
    "release_date": "2019-03-23",
  },
}
client.Index("movies").AddDocument(documents)
# rust
let progress: Progress = client.index("movies").add_or_replace(&[
  Movie {
    id: 287947,
    title: "Shazam".to_string(),
    poster: "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg".to_string(),
    overview: "A boy is given the ability to become an adult superhero in times of need with a single magic word.".to_string(),
    release_date: "2019-03-23".to_string(),
  }
], None).await.unwrap();
# swift
let path = Bundle.main.url(forResource: "movies", withExtension: "json")
let documents: Data = Data(contentsOf: path)
client.index("movies").addDocuments(documents: documents) { (result: Result<Task, Swift.Error>) in
    switch result {
    case .success(let task):
        print(task)
    case .failure(let error):
        print(error)
    }
}
# dart
await client.index('movies').addDocuments([
  {
    'id': 287947,
    'title': 'Shazam',
    'poster':
        'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
    'overview':
        'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
    'release_date': '2019-03-23'
  }
]);

索引的UID

Uid 是给定索引的唯一标识符。它用于每个Indexes/:index_uid 路由,作为: index_uid 参数。Uid 在创建索引时设置。一旦为索引定义了 uid,就不能使用相同的 uid 创建另一个索引,并且不能再更改标识符。

{
  "uid": "movie",
  "createdAt": "2019-11-20T09:40:33.711324Z",
  "updatedAt": "2019-11-20T10:16:42.761858Z"
}

主键

索引是文档的集合。所有文档都有一个主键,这是一个必填字段。此字段由主键属性名称和唯一值组成。给定索引中的所有文档共享相同的主键属性,但具有不同的唯一值。索引必须知道主键的属性名。您可以为索引设置一个主键,或者让 Meilisearch 进行推断。

相关性规则

每个索引都应用自己的相关性规则。所有索引都使用以默认顺序执行的相同内置排序规则创建。添加了第一个文档之后,索引将记录属性必须如何排序。他们的重要性顺序将从他们在文件中的出现顺序中推断出来。例如,假设您的第一个文档按以下顺序列出属性:

id, title, overview, release_date

一个文档如果在标题字段中包含匹配项,将被认为比一个在其概述中只包含匹配项的文档更具相关性。您可以更改排名规则生效的顺序,或者定义自定义排名规则以首先返回某些结果。

同义词

在数据集中,您可以决定为具有相同含义的单词创建同义词。为此,可以为索引定义一组同义词。即使他们是不同的,他们也应该被同样对待。如果搜索关联词中的任何一个,将显示相同的结果。由于同义词被链接到给定的索引,它们不会应用于同一个 Meilisearch 实例上的任何其他索引。

停用词

有时候你可能想忽略文档和搜索查询中的某些单词。为此,可以为索引定义一组停止字。除非你真的需要它们,否则有些词既没有语义价值也没有上下文。此外,他们经常过于频繁(例如,在英语中的 the 或者 of )。添加到停止词列表中的单词将在搜索过程中被忽略。除了提高相关性,指定常用词作为停止词也大大提高了性能。例如,假设你想要搜索了 the great gatsby。你希望搜索到包含 great gatsby 的文件,而不希望搜索包含包含the great 或者只搜索到 the 的文档。在这种情况下,添加到停止词列表将提高性能,并使搜索结果更相关。

字段属性

默认情况下,每个文档字段都是可搜索的,并在搜索查询时返回。字段可以有以下属性中的一个或者两个,或者没有属性可以在设置中修改:

  • Searchable: Meilisearch 使用可搜索字段的内容来评估文档的相关性。
  • Displayed: 在搜索时返回的文档只包含显示的字段。

默认情况下,存储每个字段,并且不能更改此行为。