小言_互联网的博客

Elasticsearch:定制分词器(analyzer)及相关性

455人阅读  评论(0)

在许多的情况下,我们使用现有的分词器已经足够满足我们许多的业务需求,但是也有许多的情况,我们需要定制一个特定的分词器来满足我们特定的需求。我们知道要实现全文搜索,在文档被导入到 Elasticsearch 后,每个字段都需要被分析。这里就涉及到分词。如果你对分词器还不是很了解的话,那么请参考我之前的文章 “Elasticsearch: analyzer”。

一旦文档被导入到 Elasticsearch 之后,我们就可以对其中的字段进行搜索了。通常它会依据默认的 BM25 算法对每个文档的相关性进行打分。关于每个文档的分数是如何得到的,我们可以参照文档 “Elasticsearch:分布式计分” 来了解更多。这个打分会影响搜索返回结果的先后顺序。打分最高的文档排在返回结果的最前面,紧接着是排名第二的分数,依次类推。默认的 BM25 打分规则虽然能满足我们绝大多数的需求,但是在实际的使用中,有时不能完全满足我们的需求,比如我希望一首排名靠前的歌曲的会影响最终的得分,离我们位置最近的新闻排在前面,最近发生的新闻优先排在许多年前的新闻之前。针对这些特殊的需求,我们需要定制分数的算法。

在今天的展示中,我将展示如何实现一个定制的分词器 (custom analyzer)及定制相关性。

 

安装

如果你还没有安装好自己的 Elastic Stack, 请参阅我之前的文章  “Elastic:菜鸟上手指南” 安装好自己的 Elasticsearch 及 Kibana。

 

定制 analyzer

默认情况下,如果未应用自定义设置,Elasticsearch 将使用 “standard” 分词器分析输入文本。比如:


  
  1. POST _ analyze
  2. {
  3. "text": "Hélène Ségara it's !<>#"
  4. }

在上面的字符串是一些外文的文字。它们看起来很不整齐,而且其中还有一些除字母数字之外的符号。上面的返回结果为:


  
  1. {
  2. "tokens" : [
  3. {
  4. "token" : "hélène",
  5. "start_offset" : 0,
  6. "end_offset" : 6,
  7. "type" : "<ALPHANUM>",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "ségara",
  12. "start_offset" : 7,
  13. "end_offset" : 13,
  14. "type" : "<ALPHANUM>",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "it's",
  19. "start_offset" : 14,
  20. "end_offset" : 18,
  21. "type" : "<ALPHANUM>",
  22. "position" : 2
  23. }
  24. ]
  25. }

上面使用 standard 分词器把文本进行了分词。当我们搜索时按照上面的 token 进行搜索,就可以搜索到这个文档。

下面,我们来输入 4 个文档:


  
  1. POST content/_ bulk
  2. { "index":{ "_id": "a1"}}
  3. { "type": "ARTIST", "artist_id": "a1", "artist_name": "Sezen Aksu", "ranking":10}
  4. { "index":{ "_id": "a2"}}
  5. { "type": "ARTIST", "artist_id": "a2", "artist_name": "Selena Gomez", "ranking":100}
  6. { "index":{ "_id": "a3"}}
  7. { "type": "ARTIST", "artist_id": "a3", "artist_name": "Shakira", "ranking":10}
  8. { "index":{ "_id": "a4"}}
  9. { "type": "ARTIST", "artist_id": "a4", "artist_name": "Hélène Ségara", "ranking":1000}

上面是假设的一个音乐库里的文档。它包含了艺术家的 id,艺术家的名字,以及艺术家的 ranking,也就是排名。执行上面的命令。这样我们就把上面的 4 个文档导入到 Elasticsearch 中去了。

假设我们在手机里有如下的一个搜索界面:

在上面,我们在输入字母 c 的时候,马上就有以 c 开头的所有艺术家的名字列表出来供我们来选择。针对我们的情况,我们打入如下的命令来搜索 artist_name 是以 s 开头的:


  
  1. POST content/_ search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "s",
  6. "fields": [
  7. "artist_name"
  8. ]
  9. }
  10. }
  11. }

上面的搜索将不会返回任何的内容。这个结果一点也不奇怪,因为所有的字段里的分词没有一个是含有 "s" 的 token。

我们下面先用一个例子来进行展示如何创建一个定制的 analyzer。首先,我们可以使用 pattern_replace 这个 char_filter 来替换其中的一些字符:


  
  1. POST _ analyze
  2. {
  3. "text": "Hélène Ségara it's !<>#",
  4. "char_filter": [
  5. {
  6. "type": "pattern_replace",
  7. "pattern": "[^\\s\\p{L}\\p{N}]",
  8. "replacement": ""
  9. }
  10. ],
  11. "tokenizer": "standard"
  12. }

在这里的 pattern 它使用了 regular expression。你可以参阅链接了解更多关于 Java Regular Expressions。上面的意思是替换任何以空格开始的但不是字母和数字的字符为空字符。上面命令运行的结果为:


  
  1. {
  2. "tokens" : [
  3. {
  4. "token" : "Hélène",
  5. "start_offset" : 0,
  6. "end_offset" : 6,
  7. "type" : "<ALPHANUM>",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "Ségara",
  12. "start_offset" : 7,
  13. "end_offset" : 13,
  14. "type" : "<ALPHANUM>",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "its",
  19. "start_offset" : 14,
  20. "end_offset" : 18,
  21. "type" : "<ALPHANUM>",
  22. "position" : 2
  23. }
  24. ]
  25. }

在上,我们可以看到有大写的字母在最后的 token 里,我们可以使用如下的方法来把所有的字母都变为小写的字母:


  
  1. POST _ analyze
  2. {
  3. "text": "Hélène Ségara it's !<>#",
  4. "char_filter": [
  5. {
  6. "type": "pattern_replace",
  7. "pattern": "[^\\s\\p{L}\\p{N}]",
  8. "replacement": ""
  9. }
  10. ],
  11. "tokenizer": "standard",
  12. "filter": [
  13. "lowercase"
  14. ]
  15. }

在上面的 filter 中,我们添加了 lowercase。这个 filter 它可以把所的字母都变为小写字母。上面命令输出的结果是:


  
  1. {
  2. "tokens" : [
  3. {
  4. "token" : "hélène",
  5. "start_offset" : 0,
  6. "end_offset" : 6,
  7. "type" : "<ALPHANUM>",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "ségara",
  12. "start_offset" : 7,
  13. "end_offset" : 13,
  14. "type" : "<ALPHANUM>",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "its",
  19. "start_offset" : 14,
  20. "end_offset" : 18,
  21. "type" : "<ALPHANUM>",
  22. "position" : 2
  23. }
  24. ]
  25. }

从上面我们可以看出所有的 token 都变为小写的字母了。我们也可以看到其中的一些很怪的字母,比如 é。这个字母在英文字母中不可见。我们可以通过  asciifolding 这个过滤器来对它进行处理:


  
  1. POST _ analyze
  2. {
  3. "text": "Hélène Ségara it's !<>#",
  4. "char_filter": [
  5. {
  6. "type": "pattern_replace",
  7. "pattern": "[^\\s\\p{L}\\p{N}]",
  8. "replacement": ""
  9. }
  10. ],
  11. "tokenizer": "standard",
  12. "filter": [
  13. "lowercase",
  14. "asciifolding"
  15. ]
  16. }

在上面,我们添加了 asciifolding 过滤器,那么它的输出为:


  
  1. {
  2. "tokens" : [
  3. {
  4. "token" : "helene",
  5. "start_offset" : 0,
  6. "end_offset" : 6,
  7. "type" : "<ALPHANUM>",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "segara",
  12. "start_offset" : 7,
  13. "end_offset" : 13,
  14. "type" : "<ALPHANUM>",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "its",
  19. "start_offset" : 14,
  20. "end_offset" : 18,
  21. "type" : "<ALPHANUM>",
  22. "position" : 2
  23. }
  24. ]
  25. }

从上面的输出中,我们可以看到所有的字母都变为小写,并且都是英文字母。接下来,我们希望对其中的每个 token 进行更进一步的分词,从而使它们变为可以搜索的 token,比如 'h', 'he', 'hel', 'hele'。这个我们需要使用到 ngram。你可以参阅我之前的文档 “Elasticsearch: Ngrams, edge ngrams, and shingles”。


  
  1. POST _analyze
  2. {
  3. "text": "Hélène Ségara it's !<>#",
  4. "char_filter": [
  5. {
  6. "type": "pattern_replace",
  7. "pattern": """[^\s\p{L}\p{N}]""",
  8. "replacement": ""
  9. }
  10. ],
  11. "tokenizer": "standard",
  12. "filter": [
  13. "lowercase",
  14. "asciifolding",
  15. {
  16. "type": "edge_ngram",
  17. "min_gram": "1",
  18. "max_gram": "12"
  19. }
  20. ]
  21. }

上面的命令输出的结果为:


  
  1. {
  2. "tokens" : [
  3. {
  4. "token" : "h",
  5. "start_offset" : 0,
  6. "end_offset" : 6,
  7. "type" : "<ALPHANUM>",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "he",
  12. "start_offset" : 0,
  13. "end_offset" : 6,
  14. "type" : "<ALPHANUM>",
  15. "position" : 0
  16. },
  17. {
  18. "token" : "hel",
  19. "start_offset" : 0,
  20. "end_offset" : 6,
  21. "type" : "<ALPHANUM>",
  22. "position" : 0
  23. },
  24. {
  25. "token" : "hele",
  26. "start_offset" : 0,
  27. "end_offset" : 6,
  28. "type" : "<ALPHANUM>",
  29. "position" : 0
  30. },
  31. {
  32. "token" : "helen",
  33. "start_offset" : 0,
  34. "end_offset" : 6,
  35. "type" : "<ALPHANUM>",
  36. "position" : 0
  37. },
  38. ...

也就是说当我们输入 'h' 时, 该文档也会被搜索到。当我们输入 'he' 时,该文档,也可以被搜索到。

到现在我们是时候定义我们定制 analyzer 了。我们首先删除之前的 content 索引:

DELETE /content

我们接着使用如下的命令来创建 content 索引:


  
  1. PUT content
  2. {
  3. "settings": {
  4. "analysis": {
  5. "filter": {
  6. "front_ngram": {
  7. "type": "edge_ngram",
  8. "min_gram": "1",
  9. "max_gram": "12"
  10. }
  11. },
  12. "analyzer": {
  13. "i_prefix": {
  14. "filter": [
  15. "lowercase",
  16. "asciifolding",
  17. "front_ngram"
  18. ],
  19. "tokenizer": "standard"
  20. },
  21. "q_prefix": {
  22. "filter": [
  23. "lowercase",
  24. "asciifolding"
  25. ],
  26. "tokenizer": "standard"
  27. }
  28. }
  29. }
  30. },
  31. "mappings": {
  32. "properties": {
  33. "type": {
  34. "type": "keyword"
  35. },
  36. "artist_id": {
  37. "type": "keyword"
  38. },
  39. "ranking": {
  40. "type": "double"
  41. },
  42. "artist_name": {
  43. "type": "text",
  44. "analyzer": "standard",
  45. "index_options": "offsets",
  46. "fields": {
  47. "prefix": {
  48. "type": "text",
  49. "term_vector": "with_positions_offsets",
  50. "index_options": "docs",
  51. "analyzer": "i_prefix",
  52. "search_analyzer": "q_prefix"
  53. }
  54. },
  55. "position_increment_gap": 100
  56. }
  57. }
  58. }
  59. }

在上面,有两个部分:settings 及 mappings。在 settings 的部分,它定义两个分词器: i_prefix 及 q_prefix。它们分别是 input,也就是导入文档时要使用的分词器,而 q_prefix 则指的是在 query,也就是在搜索时使用的分词器。如果大家读这个还不是很明白的话,请参阅我之前的文档  “Elasticsearch: analyzer”。在 mappings 里,针对 content,它是一个 multi-field 的字段。除了 content 可以被正常搜索以外,我们添加 content.prefix 字段。针对这个字段,在导入时使用 i_prefix 分词器,而对搜索文字来说,它使用 q_prefix 分词器。

接下来,我们使用先前的方法来重新导入之前的四个文档:


  
  1. POST content/_ bulk
  2. { "index":{ "_id": "a1"}}
  3. { "type": "ARTIST", "artist_id": "a1", "artist_name": "Sezen Aksu", "ranking":10}
  4. { "index":{ "_id": "a2"}}
  5. { "type": "ARTIST", "artist_id": "a2", "artist_name": "Selena Gomez", "ranking":100}
  6. { "index":{ "_id": "a3"}}
  7. { "type": "ARTIST", "artist_id": "a3", "artist_name": "Shakira", "ranking":10}
  8. { "index":{ "_id": "a4"}}
  9. { "type": "ARTIST", "artist_id": "a4", "artist_name": "Hélène Ségara", "ranking":1000}

一旦数据被导入,我们可以使用如下的命令来对文档进行搜索:


  
  1. POST content/_ search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "s",
  6. "fields": [
  7. "artist_name.prefix"
  8. ]
  9. }
  10. }
  11. }

我们可以看到 4 个文档都被搜索到了。我们可以打入如下的搜索:


  
  1. POST content/_ search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "se",
  6. "fields": [
  7. "artist_name.prefix"
  8. ]
  9. }
  10. }
  11. }

上面的命令输出的结果为:


  
  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.3359957,
  16. "hits" : [
  17. {
  18. "_index" : "content",
  19. "_type" : "_doc",
  20. "_id" : "a1",
  21. "_score" : 0.3359957,
  22. "_source" : {
  23. "type" : "ARTIST",
  24. "artist_id" : "a1",
  25. "artist_name" : "Sezen Aksu",
  26. "ranking" : 10
  27. }
  28. },
  29. {
  30. "_index" : "content",
  31. "_type" : "_doc",
  32. "_id" : "a2",
  33. "_score" : 0.30920535,
  34. "_source" : {
  35. "type" : "ARTIST",
  36. "artist_id" : "a2",
  37. "artist_name" : "Selena Gomez",
  38. "ranking" : 100
  39. }
  40. },
  41. {
  42. "_index" : "content",
  43. "_type" : "_doc",
  44. "_id" : "a4",
  45. "_score" : 0.29735082,
  46. "_source" : {
  47. "type" : "ARTIST",
  48. "artist_id" : "a4",
  49. "artist_name" : "Hélène Ségara",
  50. "ranking" : 1000
  51. }
  52. }
  53. ]
  54. }
  55. }

从上面的返回结果来看,索引以 se 开头的艺术家的名字都被正确地搜索到了,并返回。但是也有一些不如意的地方,比如 Sezen Aksu 的得分最高,但是他的 ranking 却只有 10,相反 Hélène Ségara 的得分最低,但是它的  ranking 却非常高。这个返回结果的 score 显然和我们的需求不太一样。如果你仔细阅读一些文章 “Elasticsearch:分布式计分”,你就会发现 Sezen 的字符串长度比 Ségara 要短。这也是它为什么得分比较高的原因。接下来,我们来通过一些算法来定制相关性。

 

定制相关性 (relevance)

所谓的相关性,也就是在搜索返回时的 score。相关性越高,那么得分就越高,它就会排在返回结果的前面,我们可通过 function_score 来定制相关性。为了能够让分数和 rangking 这个字段能有效地结合起来。我们希望 ranking 的值越高,能够在最终的得分钟起到一定的影响。我们可以通过这样的写法:


  
  1. POST content/_ search
  2. {
  3. "from": 0,
  4. "size": 10,
  5. "query": {
  6. "function_score": {
  7. "query": {
  8. "multi_match": {
  9. "query": "se",
  10. "fields": [
  11. "artist_name.prefix"
  12. ]
  13. }
  14. },
  15. "functions": [
  16. {
  17. "filter": {
  18. "match_all": {
  19. "boost": 1
  20. }
  21. },
  22. "script_score": {
  23. "script": {
  24. "source": "Math.max(((!doc['ranking'].empty)? Math.log10(doc['ranking'].value) : 1), 1)",
  25. "lang": "painless"
  26. }
  27. }
  28. }
  29. ],
  30. "boost": 1,
  31. "boost_mode": "multiply",
  32. "score_mode": "multiply"
  33. }
  34. },
  35. "sort": [
  36. {
  37. "_score": {
  38. "order": "desc"
  39. }
  40. }
  41. ]
  42. }

在上面的第一部分:


  
  1. "query": {
  2. "multi_match": {
  3. "query": "se",
  4. "fields": [
  5. "artist_name.prefix"
  6. ]
  7. }
  8. },

我们对文档含有 se 为开头的字符串文档进行搜索。在第二部分:


  
  1. "functions": [
  2. {
  3. "filter": {
  4. "match_all": {
  5. "boost": 1
  6. }
  7. },
  8. "script_score": {
  9. "script": {
  10. "source": "Math.max(((!doc['ranking'].empty)? Math.log10(doc['ranking'].value) : 1), 1)",
  11. "lang": "painless"
  12. }
  13. }
  14. }
  15. ],

我们针对 ranking 使用了自己的一个算法并得出来一个分数。在第三部分:


  
  1. "boost": 1,
  2. "boost_mode": "multiply",
  3. "score_mode": "multiply"

我们使用刚才得到的分数和之前搜索得到得分进行相乘,并得出来最后的分数。基于这种算法,ranking 越高,给搜索匹配得出来的分数的加权值就越高。从某种程度上讲,ranking 的大小会影响最终的排名。

经过上面的改造之后,最后的排名为:


  
  1. {
  2. "took" : 4,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.9777223,
  16. "hits" : [
  17. {
  18. "_index" : "content",
  19. "_type" : "_doc",
  20. "_id" : "a4",
  21. "_score" : 0.9777223,
  22. "_source" : {
  23. "type" : "ARTIST",
  24. "artist_id" : "a4",
  25. "artist_name" : "Hélène Ségara",
  26. "ranking" : 1000
  27. }
  28. },
  29. {
  30. "_index" : "content",
  31. "_type" : "_doc",
  32. "_id" : "a2",
  33. "_score" : 0.6778009,
  34. "_source" : {
  35. "type" : "ARTIST",
  36. "artist_id" : "a2",
  37. "artist_name" : "Selena Gomez",
  38. "ranking" : 100
  39. }
  40. },
  41. {
  42. "_index" : "content",
  43. "_type" : "_doc",
  44. "_id" : "a1",
  45. "_score" : 0.36826363,
  46. "_source" : {
  47. "type" : "ARTIST",
  48. "artist_id" : "a1",
  49. "artist_name" : "Sezen Aksu",
  50. "ranking" : 10
  51. }
  52. }
  53. ]
  54. }
  55. }

这一次,我们看到 Hélène Ségara 排到了第一名。

在实际的使用中,由于有海量的数据,scripts 的计算会影响搜索的速度。我们可以针对一个用所关心的歌曲进行过滤:


  
  1. POST /content/_ search
  2. {
  3. "from": 0,
  4. "size": 10,
  5. "query": {
  6. "function_score": {
  7. "query": {
  8. "multi_match": {
  9. "query": "s",
  10. "fields": [
  11. "artist_name.prefix"
  12. ]
  13. }
  14. },
  15. "functions": [
  16. {
  17. "filter": {
  18. "terms": {
  19. "artist_id": [
  20. "a4",
  21. "a3"
  22. ]
  23. }
  24. },
  25. "script_score": {
  26. "script": {
  27. "source": "params.boosts.get(doc[params.artistIdFieldName].value)",
  28. "lang": "painless",
  29. "params": {
  30. "artistIdFieldName": "artist_id",
  31. "boosts": {
  32. "a4": 5,
  33. "a3": 2
  34. }
  35. }
  36. }
  37. }
  38. }
  39. ],
  40. "boost": 1,
  41. "boost_mode": "multiply",
  42. "score_mode": "multiply"
  43. }
  44. },
  45. "sort": [
  46. {
  47. "_score": {
  48. "order": "desc"
  49. }
  50. }
  51. ]
  52. }

在上面,比如针对不同的用户,这里的 artist_id 的列表将会发送改变。这样修改的结果可以节省 script 的运算,从而提高搜索的速度。

 


转载:https://blog.csdn.net/UbuntuTouch/article/details/114278163
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场