配置自定义筛选器 Elasticsearch
在代理中使用自定义筛选器作为高级设置,以启用更高级的搜索技术。 配置自定义筛选器,将其用作适用于所有用户查询的全局筛选器。
下面的代码片段展示了自定义过滤器的一个示例:
[
{
"bool": {
"must": [
{
"term": {
"title": "A title"
}
}
],
}
}
]
如果还将自定义查询正文配置为高级 Elasticsearch 设置,则在查询正文中包含 $FILTER 变量,以应用自定义筛选器。
下面的代码片段显示了一个使用 $FILTER 变量的查询正文示例:
{
"query": {
"bool": {
"should": [
{
"text_expansion": {
"ml.tokens": {
"model_id": ".elser_model_2_linux-x86_64",
"model_text": "$QUERY"
}
}
}
],
"filter": "$FILTER"
}
}
}
过滤器对象示例
探索使用自定义筛选器过滤搜索结果的用例。 以下示例假定 Elasticsearch 索引中包含 title、 text 和 id 字段。 其中, title 和 text 是文本类型字段,而 id 是关键字类型字段。
AND
[
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
},
{
"match": {
"id": "A_specific_ID"
}
}
]
此筛选器对象通过以下条件筛选搜索结果:
title包含 "A_keyword_in_title",并且text包含 "A_keyword_in_text",并且id等于 "A_specific_ID"。
或
[
{
"bool": {
"should": [
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
},
{
"match": {
"id": "A_specific_ID"
}
}
]
}
}
]
此筛选器对象通过以下条件筛选搜索结果:
title包含 "A_keyword_in_title",或text包含 "A_keyword_in_text",或者id等于 "A_specific_ID"。
非
[
{
"bool": {
"must_not": [
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
},
{
"match": {
"id": "A_specific_ID"
}
}
]
}
}
]
此筛选器对象通过以下条件筛选搜索结果:
title不包含 "A_keyword_in_title",并且text不包含 "A_keyword_in_text",并且id不等于 "A_specific_ID"。
(a 且 b) 或 c
[
{
"bool": {
"should": [
{
"match": {
"id": "A_specific_ID"
}
}
{
"bool": {
"filter": [
{
"match": {
"title": "A_keyword_in_title"
}
},
{
"match": {
"text": "A_keyword_in_text"
}
}
]
}
}
]
}
}
]
此筛选器对象通过以下条件筛选搜索结果:
title包含 "A_keyword_in_title "和 包含 "A_keyword_in_text",或textid不等于 "A_specific_ID"
有关此 bool 查询的更多信息,请参阅 Elasticsearch 文档中的 “布尔查询”部分。