频道
bg

Algolia

coding一月 01, 20221mins

索引H1

时间日期H2

如果根据时间来进行过滤和排序,必须将数据格式化为Unix时间戳(比如1435735848

FacetingH1

Faceting相当于类别属性,可以用来过滤结果集。

搜索H1

设置可搜索属性,需要配置searchableAttributes

tsx

index.setSettings({
searchableAttributes: [
'title',
'director',
'actor'
]
}).then(() => {
// done
});

匹配H2

algolia使用分层的textual matching criteria的匹配策略,包括

  • Typo
  • Geo
  • Words
  • Filters
  • Proximity
  • Attribute
  • Exact
  • Custom

排序H2

Algolia的结果集排序包含Ranking和Sorting

Ranking策略H3

Ranking使用和textual matching criteria一样的策略,按照顺序进行分层排序,即每层的排序只是针对上一层排序后同一顺序的组内排序。这里的组内排序的概念很重要的,下一层Ranking的目标不是针对上一层的全局结果,而是针对上一层的相同的结果进行排序,和SQL中的Sort By A,B是类似的。

Custom

Custom Rank是优先级最低的,可以认为是对在默认的Rank排序后相同顺序的结果再次进行业务的排序,例如文章的创建时间等。

tsx

index.setSettings({
customRanking: [
'desc(retweets)',
'desc(likes)'
]
}).then(() => {
// done
});

SortingH2

Sorting发生在Sorting的之后,可以认为是一种强排序,Algolia提供两种排序类型

  • Exhaustive or hard sorting
  • Relevant sorting

Exhaustive or hard sortingH3

这种方式会把所有的相关结果都返回,而不考虑相关度的阈值。Exhaustive Sorting 包含三部分

  • Sort-by attributes (optional)
  • Textual ranking criteria
  • Custom ranking, 也叫做business ranking criteria

Untitled

这里Algolia先使用Sort-by对整个结果集排序,确保全局的业务顺序,但是不影响的相关度的。而Custom Ranking最后进行,只是对TEXUAL排序后相同的结果进行组内排序。

Relevant sortingH3

Relevant sorting 只返回满足[relevancyStrictness](https://www.algolia.com/doc/api-reference/api-parameters/relevancyStrictness/) 设置的相关性阈值的结果。

过滤H1

使用过滤器

tsx

// Only "Motorola" smartphones
index.search('smartphone', {
filters: 'brand:Motorola'
}).then(({ hits }) => {
console.log(hits);
});

其中filters 声明的过滤条件对于不同的数据类型,语法会稍许不同

  • 字符串 字符串过滤只能是针对Faceting,所以需要先声明Faceting

    tsx

    index.setSettings({
    attributesForFaceting: [
    'brand' // or 'filterOnly(brand)' for filtering purposes only
    ]
    }).then(() => {
    // done
    });
  • 数字 filters: price < 100“
  • 布尔 filters: is_available:true“
  • 日期 filters: date_timestamp > ${Math.floor(d.setDate(d.getDate() - 7) / 1000)}“
  • 布尔操作 filters: '(category:Book OR category:Ebook) AND NOT author:"JK Rowling"'

AutoComplete 客户端

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.