面试题答案
一键面试1. 映射设置
- 中文字段:
{ "properties": { "chinese_text": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart", "fields": { "synonyms": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart", "synonyms_path": "synonyms_zh.txt" } } } } }
- 使用
ik_max_word
作为分析器进行索引,ik_smart
作为搜索分析器,适合中文分词。fields.synonyms
子字段用于处理同义词,通过synonyms_path
指定同义词文件。
- 使用
- 英文字段:
{ "properties": { "english_text": { "type": "text", "analyzer": "english", "fields": { "synonyms": { "type": "text", "analyzer": "english", "synonyms": ["big => large, great", "small => little"] } } } } }
- 使用
english
分析器,它会对英文进行词干提取等操作。同义词直接在映射中定义。
- 使用
- 法文字段:
{ "properties": { "french_text": { "type": "text", "analyzer": "french", "fields": { "synonyms": { "type": "text", "analyzer": "french", "synonyms_path": "synonyms_fr.txt" } } } } }
- 使用
french
分析器,针对法文进行处理,通过synonyms_path
指定同义词文件。
- 使用
2. 搜索策略
- 全文搜索:
- 针对单语言搜索,例如搜索中文,使用
match
查询:
{ "query": { "match": { "chinese_text": "要搜索的中文内容" } } }
- 英文和法文类似,只需替换对应的字段名。
- 针对单语言搜索,例如搜索中文,使用
- 同义词处理:
- 搜索时,Elasticsearch会自动根据映射中的同义词设置,将搜索词扩展为同义词进行搜索。例如,搜索英文时,若设置了
big => large, great
,搜索big
会同时搜索large
和great
。
- 搜索时,Elasticsearch会自动根据映射中的同义词设置,将搜索词扩展为同义词进行搜索。例如,搜索英文时,若设置了
- 跨语言联合搜索:
- 使用
multi - match
查询:
{ "query": { "multi_match": { "query": "要搜索的内容", "fields": ["chinese_text", "english_text", "french_text"] } } }
- 此查询会在多个语言字段中同时搜索,实现跨语言联合搜索。可以根据需求调整
type
参数,如best_fields
(默认,返回最佳匹配字段的结果)、most_fields
(匹配多个字段的结果)等。
- 使用