美文网首页
elasticsearch中英文查询

elasticsearch中英文查询

作者: 陈文瑜 | 来源:发表于2019-08-06 09:38 被阅读0次

插件安装

  • 安装IK Analysis插件 下载
    mkdir /plugins/ik
    #解压
    elasticsearch-analysis-ik.zip
    
  • pinyin插件 下载
    mkdir /plugins/pinyin
    #解压
    elasticsearch-analysis-pinyin-6.8.1.zip
    

ik使用

  • 建索引
    PUT /index
    #设置分析方式
    POST /index/fulltext/_mapping
    {
      "properties": {
        "content":{
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
    
  • 插入数据
    POST /index/fulltext/1
    {"content":"美国留给伊拉克的是个烂摊子吗"}
    
    POST /index/fulltext/2
    {"content":"公安部:各地校车将享最高路权"}
    
    POST /index/fulltext/3
    {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
    
    POST /index/fulltext/4
    {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
    
  • 查询
    POST /index/fulltext/_search
    {
      "query": {
        "match": {
          "content": "中国"
        }
      },
      "highlight": {
        "pre_tags": ["<tag1>","<tag2>"],
        "post_tags": ["</tag1>","</tag2>"],
        "fields": {
          "content": {}
        }
      }
    }
    

pinyin使用

  • 自定义拼音分析器创建索引
    PUT /medcl/ 
    {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "pinyin_analyzer" : {
                        "tokenizer" : "my_pinyin"
                        }
                },
                "tokenizer" : {
                    "my_pinyin" : {
                        "type" : "pinyin",
                        "keep_separate_first_letter" : false,
                        "keep_full_pinyin" : true,
                        "keep_original" : true,
                        "limit_first_letter_length" : 16,
                        "lowercase" : true,
                        "remove_duplicated_term" : true
                    }
                }
            }
        }
    }
    # 分析
    GET /medcl/_analyze
    {
      "text": ["刘德华"],
      "analyzer": "pinyin_analyzer"
    }
    
  • 创建映射mapping
    POST /medcl/folks/_mapping 
    {
        "folks": {
            "properties": {
                "name": {
                    "type": "keyword",
                    "fields": {
                        "pinyin": {
                            "type": "text",
                            "store": false,
                            "term_vector": "with_offsets",
                            "analyzer": "pinyin_analyzer",
                            "boost": 10
                        }
                    }
                }
            }
        }
    }
    
  • 插入数据并查询
    POST /medcl/folks/andy 
    {"name":"刘德华"}
    
    #查询
    GET /medcl/_search
    {
      "query": {
        "match": {
          "name.pinyin": "刘dehua"
        }
      }
    }
    

相关文章

网友评论

      本文标题:elasticsearch中英文查询

      本文链接:https://www.haomeiwen.com/subject/oonbdctx.html