Using Elasticsearch ik participle to implement synonym search

Source: Internet
Author: User

1, first need to install good elasticsearch and Elasticsearch-analysis-ik word breaker

2. Configure IK synonyms

The Elasticsearch comes with a synonym filter named synonym. In order for IK and synonym to work together, we need to define a new analyzer and use IK as the tokenizer,synonym filter. It sounds complicated, but the only thing to do is add a configuration.

Open the/config/elasticsearch.yml file and add the following configuration:

[HTML]View PlainCopy 
  1. Index
  2. Analysis
  3. Analyzer
  4. Ik_syno:
  5. Type:custom
  6. Tokenizer:ik_max_word
  7. Filter: [My_synonym_filter]
  8. Ik_syno_smart:
  9. Type:custom
  10. Tokenizer:ik_smart
  11. Filter: [My_synonym_filter]
  12. Filter
  13. My_synonym_filter:
  14. Type:synonym
  15. Synonyms_path:analysis/synonym.txt

The above configuration defines the two new analyzer Ik_syno and Ik_syno_smart, respectively, corresponding to IK's ik_max_word and ik_smart two word segmentation strategies. According to the IK documentation, the differences are as follows:

    • Ik_max_word: The text will be the most fine-grained split, for example, "the national anthem of the People's Republic of China" will be divided into "People's Republic of China, the Chinese people, Chinese, Chinese, People's Republic, people, people, peoples, Republics, Republic, and, Juhua, national anthem", will exhaust all possible combinations;
    • Ik_smart: The text will be the most coarse-grained split, such as "the national anthem of the People's Republic of China" will be split into "People's Republic of China, the national anthem";

Both Ik_syno and Ik_syno_smart use the synonym filter to implement synonym conversions.

3, create the/config/analysis/synonym.txt file, enter some synonyms and coexist in the utf-8 format. For example

To this synonym configuration has been completed, restart ES, search for the specified word is Ik_syno or ik_syno_smart.

Create a mapping map. Execute the Curl command as follows

[HTML]View PlainCopy 
  1. Curl-xpost http://192.168.1.99:9200/goodsindex/goods/_mapping-d ' {
  2. "Goods": {
  3. "_all": {
  4. "Enabled": True,
  5. "Analyzer": "Ik_max_word",
  6. "Search_analyzer": "Ik_max_word",
  7. "Term_vector": "No",
  8. "Store": "false"
  9. },
  10. "Properties": {
  11. "title": {
  12. ' Type ': ' String ',
  13. "Term_vector": "With_positions_offsets",
  14. "Analyzer": "Ik_syno",
  15. "Search_analyzer": "Ik_syno"
  16. },
  17. "Content": {
  18. ' Type ': ' String ',
  19. "Term_vector": "With_positions_offsets",
  20. "Analyzer": "Ik_syno",
  21. "Search_analyzer": "Ik_syno"
  22. },
  23. "Tags": {
  24. ' Type ': ' String ',
  25. "Term_vector": "No",
  26. "Analyzer": "Ik_syno",
  27. "Search_analyzer": "Ik_syno"
  28. },
  29. "slug": {
  30. ' Type ': ' String ',
  31. "Term_vector": "No"
  32. },
  33. "Update_date": {
  34. "Type": "Date",
  35. "Term_vector": "No",
  36. "Index": "No"
  37. }
  38. }
  39. }
  40. }‘

The above code specifies the field characteristics for the article type under the test index: the title, content, and tags fields use Ik_syno as the analyzer, indicating that it uses Ik_max_word as a word breaker and applies synonym synonyms The slug field does not specify an analyzer, indicating that it uses the default word breaker, and that the Update_date field is not indexed.

Using Elasticsearch ik participle to implement synonym search (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.