Testing analyzers(测试分析器)

analyze API是查看分析器生成词语的一个非常有用的工具。内置的分析器(或结合内置的分词器,词语过滤器和字符过滤器)可以在请求中指定内联:

  1. POST _analyze
  2. {
  3. "analyzer": "whitespace",
  4. "text": "The quick brown fox."}
  5. POST _analyze
  6. {
  7. "tokenizer": "standard",
  8. "filter": [ "lowercase", "asciifolding" ],
  9. "text": "Is this déja vu?"}
Positions and character offsets(位置和字符偏移)

从analyze API的输出可以看出,分析器不仅将单词转换为词语,还记录了每个词语(用于短语查询或近义词查询)的顺序或相对位置,以及原始文本中每个词语的起始和结束字符的偏移量(用于突出搜索片段)。

或者,在特定的索引上运行analyze API时,可以参考自定义分析器:

  1. PUT my_index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "std_folded": { #1
  7. "type": "custom",
  8. "tokenizer": "standard",
  9. "filter": [
  10. "lowercase",
  11. "asciifolding"
  12. ]
  13. }
  14. }
  15. }
  16. },
  17. "mappings": {
  18. "my_type": {
  19. "properties": {
  20. "my_text": {
  21. "type": "text",
  22. "analyzer": "std_folded" #2
  23. }
  24. }
  25. }
  26. }}
  27. GET my_index/_analyze { #3
  28. "analyzer": "std_folded", #4
  29. "text": "Is this déjà vu?"}
  30. GET my_index/_analyze { #5
  31. "field": "my_text", #6
  32. "text": "Is this déjà vu?"}

| 1 | 定义名为std_folded的自定义分析器。 | | 2 | 字段my_text使用std_folded分析器。 | | 3,5 | 要参考此分析器,analyze API必须指定索引名称。 | | 4 | 按名称查看分析器。 | | 6 | 请参阅字段my_text使用的分析器。 |

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/getting-started.html(修改该链接为官网对应的链接)

译文链接 : http://www.apache.wiki/display/Elasticsearch(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 曾少峰ApacheCNApache中文网