索引配置

本页介绍在 Meilisearch 可用的索引级别设置以及如何自定义这些设置。

displayed 属性

将属性添加到 displayed-attributes 列表 的字段包含在每个匹配文档中。

在搜索时返回的文档只包含显示的字段。

displayedAttributes=[<String>, <String>, ...]

//[<String>, <String>, ...] 字符串数组
//默认为文档中找到的所有属性。包含要显示的索引属性的字符串数组。

示例

通过添加以下设置,搜索返回的文档将包含字段title、overview、genres和release_date。

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/movies/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "displayedAttributes": [
          "title",
          "overview",
          "genres",
          "release_date"
      ]
  }'
# JS
client.index('movies').updateSettings({
  displayedAttributes: [
    'title',
    'description',
    'genre',
    'release_date',
  ]
})

# python
client.index('movies').update_settings({
  'displayedAttributes': [
    'title',
    'description',
    'genre',
    'release_date'
  ]
})

# php
$client->index('movies')->updateSettings([
  'displayedAttributes' => [
    'title',
    'description',
    'genre',
    'release_date'
  ]
]);

# java
Settings settings = new Settings();
settings.setDisplayedAttributes(new String[]
{
  "title",
  "description",
  "genre",
  "release_date"
});
client.index("movies").updateSettings(settings);

# ruby
client.index('movies').update_settings({
  displayed_attributes: [
    'title',
    'description',
    'genre',
    'release_date'
  ]
})

# go
settings := meilisearch.Settings{
  DisplayedAttributes: []string{
    "title",
    "description",
    "genre",
    "release_date",
  },
}
client.Index("movies").UpdateSettings(&settings)

# rust
let settings = Settings::new()
  .with_displayed_attributes([
    "title",
    "description",
    "genre",
    "release_date"
  ]);

let progress: Progress = client.index("movies").set_settings(&settings).await.unwrap();

# swift
let settings = Setting(
  displayedAttributes: [
    "title",
    "description",
    "poster",
    "release_date",
    "rank"
])
client.index("movies").updateSettings(settings) { (result: Result<Update, Swift.Error>) in
    switch result {
    case .success(let update):
        print(update)
    case .failure(let error):
        print(error)
    }
}

# dart
await client.index('movies').updateSettings(IndexSettings(
    displayedAttributes: ['title', 'description', 'genre', 'release_date']));

distinct 属性

属性设置为不同属性的字段的值在返回的文档中始终是唯一的。具体细节可以阅读Distince Attribute章节

// <String> (String, defaults to null) 字段名称
distinctAttribute=<String>

示例

假设您有一个电子商务数据集。对于包含有关jackets的信息的索引,您可能有几个相同的项目,但有不同的变化(color or size)。

[
  {
    "id": 1,
    "description": "Leather jacket",
    "brand": "Lee jeans",
    "color": "brown",
    "product_id": "123456"
  },
  {
    "id": 2,
    "description": "Leather jacket",
    "brand": "Lee jeans",
    "color": "black",
    "product_id": "123456"
  }
]

您可能希望忽略项目的不同颜色。为此,可以将 product_id 设置为 distinctAttribute

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/jackets/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "distinctAttribute": "product_id"
  }

# JS
client.index('jackets').updateSettings({
  distinctAttribute: 'product_id'
})

# python
client.index('jackets').update_settings({
  'distinctAttribute': 'product_id'
})

# php
$client->index('jackets')->updateSettings([
  'distinctAttribute' => 'product_id'
]);


# java
Settings settings = new Settings();
settings.setDistinctAttribute("product_id");
client.index("jackets").updateSettings(settings);

# ruby
client.index('jackets').update_distinct_attribute('product_id')

# go
distinctAttribute := "product_id"
settings := meilisearch.Settings{
  DistinctAttribute: &distinctAttribute,
}
client.Index("jackets").UpdateSettings(&settings)

# rust
let settings = Settings::new()
  .with_distinct_attribute("product_id");

let progress: Progress = client.index("jackets").set_settings(&settings).await.unwrap();

# swift
let settings = Setting(
  distinctAttribute: "product_id"
)
client.index("jackets").updateSettings(settings) { (result: Result<Task, Swift.Error>) in
    switch result {
    case .success(let task):
        print(task)
    case .failure(let error):
        print(error)
    }
}

# dart
await client
    .index('jackets')
    .updateSettings(IndexSettings(distinctAttribute: 'product_id'));

使用上面示例中的设置,如果搜索 Lee leather jacket,则只返回两个文档中的一个

filterable 属性

可用于筛选和faceted搜索时的属性列表。

默认情况下,filterableAttributes 为空数组。它需要一个属性数组,其对应的值要么是数字,要么是字符串。空字段或包含空数组的字段将以静默方式忽略,但如果字段的值是对象,则将引发错误。

为了使用筛选器搜索参数,必须配置 filterableAttributes。

示例

为了能够过滤movie数据库中director和genres的搜索结果,你必须首先将这些属性添加到 filterableAttributes 列表中:

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/movies/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "filterableAttributes": [
          "director",
          "genres"
      ]
  }'

# JS
client.index('movies')
  .updateFilterableAttributes([
    'director',
    'genres'
  ])

# python
client.index('movies').update_filterable_attributes([
    'director',
    'genres',
])

# php
$client->index('movies')->updateFilterableAttributes(['director', 'genres']);

# java
client.index("movies").updateFilterableAttributesSettings(new String[]
{
  "director",
  "genres"
});

# ruby
client.index('movies').update_filterable_attributes([
  'director',
  'genres'
])

# go
resp, err := client.Index("movies").UpdateFilterableAttributes(&[]string{
  "director",
  "genres",
})

# rust
let progress: Progress = client.index("movies").set_filterable_attributes(["director", "genres"]).await.unwrap();

# swift
client.index("movies").updateFilterableAttributes(["genre", "director"]) { (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').updateFilterableAttributes([
  'director',
  'genres',
]);

排序规则

内置的排序规则,确保搜索结果的相关性。排序规则以默认顺序应用,可以在设置中更改该顺序。您可以添加或删除规则并更改它们的优先顺序。

//[<String>, <String>, ...]
//一个字符串数组,包含按优先顺序排序的排序规则(从最重要的规则排列到最不重要的规则)
rankingRules=[<String>, <String>, ...]

默认值(默认顺序中的排名规则) :

[
  "words", 
  "typo", 
  "proximity", 
  "attribute", 
  "sort",
  "exactness"
]

自定义排序规则

您可以在排序规则列表的任何位置添加自定义排序规则。自定义排序规则由一个属性和一个升序或降序组成。该属性在文档中必须有一个数字值。

如果某些文档不包含在自定义排序规则中定义的属性,则排序规则的应用程序是未定义的,搜索结果可能不会按预期的顺序排序

建议所有文档都包含自定义排名规则中使用的任何属性。例如,如果设置了自定义排序规则 desc(year) ,请确保所有文档都包含 year 属性。

示例

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/movies/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "rankingRules": [
          "words",
          "typo",
          "proximity",
          "attribute",
          "sort",
          "exactness",
          "release_date:asc",
          "rank:desc"
      ]
  }'
# JS
client.index('movies').updateSettings({
  rankingRules: [
      'words',
      'typo',
      'proximity',
      'attribute',
      'sort',
      'exactness',
      'release_date:asc',
      'rank:desc'
  ]
})
# python
client.index('movies').update_settings({
  'rankingRules': [
      'words',
      'typo',
      'proximity',
      'attribute',
      'sort',
      'exactness',
      'release_date:asc',
      'rank:desc'
  ]
})
# php
$client->index('movies')->updateSettings([
  'rankingRules' => [
    'words',
    'typo',
    'proximity',
    'attribute',
    'sort',
    'exactness',
    'release_date:asc',
    'rank:desc'
  ]
]);
# java
Settings settings = new Settings();
settings.setRankingRules(new String[]
{
  "words",
  "typo",
  "proximity",
  "attribute",
  "sort",
  "exactness",
  "release_date:asc",
  "rank_desc"
});
client.index("movies").updateSettings(settings);
# ruby
client.index('movies').update_settings({
  ranking_rules: [
    'words',
    'typo',
    'proximity',
    'attribute',
    'sort',
    'exactness',
    'release_date:asc',
    'rank:desc'
  ]
})
# go
settings := meilisearch.Settings{
  RankingRules: []string{
    "words",
    "typo",
    "proximity",
    "attribute",
    "sort",
    "exactness",
    "release_date:asc",
    "rank:desc",
  },
}
client.Index("movies").UpdateSettings(&settings)
# rust
let settings = Settings::new()
  .with_ranking_rules([
    "words",
    "typo",
    "proximity",
    "attribute",
    "sort",
    "exactness",
    "release_date:asc",
    "rank:desc",
  ]);

let progress = client.index("movies").set_settings(&settings).await.unwrap();
# swift
let settings = Setting(
  rankingRules: [
    "words",
    "typo",
    "proximity",
    "attribute",
    "sort",
    "exactness",
    "release_date:asc",
    "rank:desc"
])
client.index("movies").updateSettings(settings) { (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').updateSettings(IndexSettings(rankingRules: [
      'words',
      'typo',
      'proximity',
      'attribute',
      'sort',
      'exactness',
      'release_date:asc',
      'rank:desc'
    ]));

使用上面示例中的设置,文档将首先通过减少匹配查询词的数量进行排序。如果太多文档具有相同数量的查询词,则将应用排版规则。此操作将与下一条规则一起重复,直到达到所请求的文档数量(默认值为20)

searchable 属性

将属性添加到可搜索属性列表中的字段的内容搜索匹配的查询词。

// [<String>, <String>, ...]
// 字符串数组,默认为文档中找到的所有属性
searchableAttributes=[<String>, <String>, ...]

示例

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/movies/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "searchableAttributes": [
          "title",
          "overview",
          "genres"
      ]
  }'
# JS
client.index('movies').updateSettings({
  searchableAttributes: [
    'title',
    'description',
    'genre'
  ]
})
# python
client.index('movies').update_settings({
  'searchableAttributes': [
    'title',
    'description',
    'genre'
  ]
})
# php
$client->index('movies')->updateSettings([
  'searchableAttributes' => [
    'title',
    'description',
    'genre'
  ],
]);
# java
Settings settings = new Settings();
settings.setSearchableAttributes(new String[]
{
  "title",
  "description",
  "genre"
});
client.index("movies").updateSettings(settings);
# ruby
client.index('movies').update_settings({
  searchable_attributes: [
    'title',
    'description',
    'genre'
  ]
})
# go
settings := meilisearch.Settings{
  SearchableAttributes: []string{
    "title",
    "description",
    "genre",
  },
}
client.Index("movies").UpdateSettings(&settings)
# rust
let settings = Settings::new()
  .with_searchable_attributes([
    "title",
    "description",
    "genre"
  ]);

let progress: Progress = client.index("movies").set_settings(&settings).await.unwrap();
# swift
let settings = Setting(
  searchableAttributes: [
    "uid",
    "movie_id",
    "title",
    "description",
    "poster",
    "release_date",
    "rank"
])
client.index("movies").updateSettings(settings) { (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').updateSettings(
    IndexSettings(searchableAttributes: ['title', 'description', 'genre']));

Sortable属性

可用于排序的属性列表

默认情况下,sortableAttributes 为空数组。它需要一个属性数组,其对应的值可以是数字或字符串。空字段或包含空数组的字段将以默认方式忽略,但如果字段的值是对象,则将引发错误。

示例

例如:为了能够在网络商店中根据属性 price 和 author 对搜索结果进行排序,必须首先将它们添加到 sortableAttributes 列表中。

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/books/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "sortableAttributes": [
      "price",
      "author"
    ]
  }'
# JS
client.index('books').updateSettings({
  sortableAttributes: [
    'author',
    'price'
  ]
})
# python
client.index('books').update_settings({
  'sortableAttributes': [
    'price',
    'author'
  ]
})
# php
$client->index('books')->updateSettings([
  'sortableAttributes' => [
    'author',
    'price'
  ]
]);
# java
Settings settings = new Settings();
settings.setSortableAttributes(new String[]
{
  "author",
  "price",
});
client.index("books").updateSettings(settings);
# ruby
client.index('books').update_settings({
  sortable_attributes: [
    'price',
    'author'
  ]
})
# go
settings := meilisearch.Settings{
  SortableAttributes: []string{
    "author",
    "price",
  },
}
client.Index("books").UpdateSettings(&settings)
# rust
let settings = Settings::new()
  .with_sortable_attributes([
    "author",
    "price"
  ]);

let progress: Progress = client.index("books").set_settings(&settings).await.unwrap();
# swift
let settings = Setting(
  sortableAttributes: [
    "author",
    "price"
])
client.index("books").updateSettings(settings) { (result: Result<Task, Swift.Error>) in
    switch result {
    case .success(let task):
        print(task)
    case .failure(let error):
        print(error)
    }
}
# dart
await client
    .index('books')
    .updateSettings(IndexSettings(sortableAttributes: ['author', 'price']));

停用词

为索引定义的一组单词。因为有些词既没有语义价值也没有上下文,你可能想在搜索中忽略它们。在搜索过程中,停用词被忽略。

// [<String>, <String>, ...]
// 字符串数组,默认为空
stopWords=[<String>, <String>, ...]

示例

比如设置the, a 和 an 为停用词。

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/movies/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "stopWords": [
          "the",
          "a",
          "an"
      ]
  }'
# JS
client.index('movies').updateSettings({
  stopWords: [
    'the',
    'a',
    'an'
  ]
})
# python
client.index('movies').update_settings({
  'stopWords': [
      'the',
      'a',
      'an'
  ],
})
# php
$client->index('movies')->updateSettings([
  'stopWords' => [
    'the',
    'a',
    'an'
  ],
]);
# java
Settings settings = new Settings();
settings.setStopWords(new String[]
{
  "the",
  "a",
  "an"
});
client.index("movies").updateSettings(settings);
# ruby
client.index('movies').update_settings({
  stop_words: [
    'the',
    'a',
    'an'
  ]
})
# go
settings := meilisearch.Settings{
  StopWords: []string{
    "the",
    "a",
    "an",
  },
}
client.Index("movies").UpdateSettings(&settings)
# rust
let settings = Settings::new()
  .with_stop_words([
    "the",
    "a",
    "an"
  ]);

let progress = client.index("movies").set_settings(&settings).await.unwrap();
# swift
let settings = Setting(
  stopWords: [
    "the",
    "a",
    "an"
])
client.index("movies").updateSettings(settings) { (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')
    .updateSettings(IndexSettings(stopWords: ['the', 'a', 'an']));

通过上面例子中的设置,如果在搜索查询中出现了 the, a 和 an,那么排序算法就会忽略这些设置。

假设您想在movie数据库中搜索the mask。由于已经设置了 the 为停用词,所以 Meilisearch 会在movie数据库中搜索每一个包含 mask 的数据,而不是去搜索the。the 相对于 mask来说不是那么重要,the 在英语单词中也是一个非常常见的英语单词。通过把 the 添加到停用词列表,Meilisearch 在搜索的时候就会忽略 the 这个词,从而在不丢失相关性的情况下更快地响应。

同义词

为索引定义的一组词。同义词是具有相同意义的不同的词,因此处理方式是相同的。如果搜索关联词中的任何一个,将显示相同的结果。具体细节可以阅读同义词章节

// [<String>, <String>, ...]
// 对象,默认为{}, { <String>: [<String>, <String>, ...], ... }
synonyms=<Object>

示例

假设您有一个电子商务数据集。对于包含上衣信息的索引,您决定创建 sweater 和 jumper 的同义词,因为这两个可能意思非常相似。。

# cURL
curl \
  -X POST 'http://localhost:7700/indexes/tops/settings' \
  -H 'Content-Type: application/json' \
  --data-binary '{
      "synonyms": {
          "sweater": ["jumper"],
          "jumper": ["sweater"]
      }
  }'
# JS
client.index('tops').updateSettings({
  synonyms: {
    sweater: ['jumper'],
    jumper: ['sweater']
})
# python
client.index('tops').update_settings({
  'synonyms': {
    sweater: ['jumper'],
    jumper: ['sweater']
  },
})
# php
$client->index('tops')->updateSettings([
  'synonyms' => [
    'sweater' => ['jumper'],
    'jumper' => ['sweater']
  ]
]);
# java
Settings settings = new Settings();
HashMap<String, String[]> synonyms = new HashMap<String, String[]>();
synonyms.put("sweater", new String[] {"jumper"});
synonyms.put("jumper", new String[] {"sweater"});
settings.setSynonyms(synonyms);
client.index("tops").updateSettings(settings);
# ruby
client.index('tops').update_settings({
  synonyms: {
    sweater: ['jumper'],
    jumper: ['sweater']
  }
})
# go
settings := meilisearch.Settings{
  Synonyms: map[string][]string{
    "sweater": []string{"jumper"},
    "jumper":     []string{"sweater"},
  },
}
client.Index("tops").UpdateSettings(&settings)
# rust
let mut synonyms = HashMap::new();
synonyms.insert(String::from("sweater"), vec![String::from("jumper")]);
synonyms.insert(String::from("jumper"), vec![String::from("sweater")]);

let settings = Settings::new()
  .with_synonyms(synonyms);

let progress = client.index("tops").set_settings(&settings).await.unwrap();
# swift
let settings = Setting(
  synonyms: [
    "sweater": ["jumper"],
    "jumper": ["sweater"]
])
client.index("tops").updateSettings(settings) { (result: Result<Task, Swift.Error>) in
    switch result {
    case .success(let task):
        print(task)
    case .failure(let error):
        print(error)
    }
}
# dart
await client.index('tops').updateSettings(IndexSettings(synonyms: {
      'sweater': ['jumper'],
      'jumper': ['sweater']
    }));

通过上面例子中的设置,在搜索black sweater时,也会返回black jumper的搜索结果。