添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例。在下面的例子中,我取了两个同义词table和tables
I get zero results
请再次检查您的索引映射。根据下面的示例,搜索关键字是table chair,这将在title 和content 两个字段中进行搜索。下面的查询将返回同时包含table AND chair 的文档。详细解释请参考Multi match query和synonym token filter上的ES文档。
索引映射:
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
"table, tables"
]
}
},
"analyzer": {
"synonym_analyzer": {
"filter": [
"lowercase",
"synonym_filter"
],
"tokenizer": "standard"
}
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text"
}
}
}
}
索引数据:
{ "title": "table chair" }
{ "title": "tables chair" }
{ "title": "table fan" }
{ "title": "light fan", "content": "chair" }
搜索查询:
{
"query": {
"multi_match": {
"query": "table chair",
"operator": "and",
"type":"cross_fields",
"fields": [
"title","content"
],
"analyzer": "synonym_analyzer"
}
}
}
搜索结果:
"hits": [
{
"_index": "synonym",
"_type": "_doc",
"_id": "1",
"_score": 1.7227666,
"_source": {
"title": "table chair"
}
},
{
"_index": "synonym",
"_type": "_doc",
"_id": "2",
"_score": 1.3862942,
"_source": {
"title": "tables chair"
}
}
]
搜索table chair 或chair table,得到的搜索结果如上所示。