当我在elastic search中添加table和tables作为同义词筛选器时,我需要筛选出表扇的结果。如何在弹性搜索中实现这一点
我们是否可以在设置中构建包含和排除列表过滤器的分类,而不是在弹性搜索中的运行时查询
发布于 2020-09-23 15:03:18
添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
"table, tables"
]
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter"
]
}
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "synonym_analyzer",
"search_analyzer": "standard"
}
}
}
}分析应用编程接口
POST/_analyze
{
"analyzer" : "synonym_analyzer",
"text" : "table fan"
}将生成以下令牌:
{
"tokens": [
{
"token": "table",
"start_offset": 0,
"end_offset": 5,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "tables",
"start_offset": 0,
"end_offset": 5,
"type": "SYNONYM",
"position": 0
},
{
"token": "fan",
"start_offset": 6,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 1
}
]
}索引数据:
{ "title": "table and fan" }
{ "title": "tables and fan" }
{ "title": "table fan" }
{ "title": "tables fan" }
{ "title": "table chair" }搜索查询:
{
"query": {
"bool": {
"must": {
"match": {
"title": "table"
}
},
"filter": {
"bool": {
"must_not": [
{
"match_phrase": {
"title": "table fan"
}
},
{
"match_phrase": {
"title": "table and fan"
}
}
]
}
}
}
}
}也可以使用match query代替match_phrase查询
{
"query": {
"bool": {
"must": {
"match": {
"title": "table"
}
},
"filter": {
"bool": {
"must_not": [
{
"match": {
"title": {
"query": "table fan",
"operator": "AND"
}
}
}
]
}
}
}
}
}搜索结果:
"hits": [
{
"_index": "synonym",
"_type": "_doc",
"_id": "2",
"_score": 0.06783115,
"_source": {
"title": "table chair"
}
}
]更新1:
我们是否可以在设置中构建包含和排除列表过滤器的分类,而不是在弹性搜索中的运行时查询
映射是定义如何存储文档及其包含的字段的过程,并对此ES documentation on mapping执行indexed.Refer操作,以了解使用什么映射来定义。
请参阅有关Dynamic template的文档,该文档允许您定义可应用于动态添加的字段的自定义映射
发布于 2020-09-23 15:07:13
GET <indexName>/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"<fieldName>": {
"query": "table fan", // <======= Below operator will applied b/w table(&synonyms) And fan(&synonyms)
"operator": "AND"
}
}
}
]
}
}
}您可以使用上面的查询来排除所有同时具有'table‘、'fan’及其对应同义词的文档。
OR:
如果你想玩多个逻辑运算符。例如,给我所有不包含"table fan“或"ac”的文档,您可以使用simple_query_string
GET <indexName>/_search
{
"query": {
"bool": {
"must_not": [
{
"simple_query_string": {
"query": "(table + fan) | ac", // <=== '+'='and', '|'='or', '-'='not'
"fields": [
"<fieldName>" // <==== use multiple field names, wildcard also supported
]
}
}
]
}
}
}https://stackoverflow.com/questions/64022009
复制相似问题