elasticsearch实现搜索拼音然后高亮内容

es拼音插件https://github.com/medcl/elasticsearch-analysis-pinyin/tree/v1.6.0,具体用法此处不介绍,看readme


现在要实现一个搜场馆的功能,需要按场馆名称首字母缩写也能搜索出场馆,还必须要高亮显示首拼对应的汉字部分。

analysis可定义如下:

    "analysis": {
      "tokenizer": {
        "my_pinyin" : {
            "type" : "pinyin",
            "first_letter" : "only"
        }
      },
      "filter": {
        "pinyin_filter" : {
            "type" : "pinyin",
            "first_letter" : "only",
            "padding_char" : ""
        }
      },
      "analyzer": {
        "pinyin_analyzer" : {
          "tokenizer" : "ansj_query_token",
          "filter" : "pinyin_filter"
          },
        "pinyin_all_analyzer" : {
          "tokenizer" : "my_pinyin",
          "filter" : "word_delimiter"
          },
        "index_ansj": {
           "type": "custom",
           "tokenizer": "ansj_index_token"
        },
        "query_ansj": {
           "type": "custom",
           "tokenizer": "ansj_query_token"
        }
      }
    }
分词部分使用的是ansj分词。

场馆mapping如下:

{
  "properties": {
    "name": {
      "type": "string",
      "term_vector" : "with_positions_offsets",
      "index_analyzer": "index_ansj",
      "search_analyzer": "query_ansj",
      "fields": {
        "pinyin": {
          "type": "string",
          "index_analyzer": "pinyin_analyzer",
          "search_analyzer": "query_ansj"
        }
      }
    },
    "address": {
      "type": "string",
      "term_vector" : "with_positions_offsets",
      "index_analyzer": "index_ansj",
      "search_analyzer": "query_ansj"
    },
    "createTime" : {
      "type": "date",
       "format": "yyy-MM-dd HH:mm:ss"
    }
  }
}
name字段采用的multi_fields字段,创建name索引的同时也会创建name.pinyin索引。

搜索query可以这么写:

{
    "_source": ["createTime"], 
    "query": {
          "multi_match": {
              "type": "most_fields",
              "query": "cs",
              "fields": ["name", "name.pinyin","address"],
              "minimum_should_match": "-20%"
            }

        },
      "sort": [
        {"createTime":{"order":"desc"}}
      ],
    "highlight": {
      "boundary_chars":".,!? \t\n,。!?",
      "pre_tags" : ["<font color='red'>"],
      "post_tags" : ["</font>"],
      "fields": {
        "name" : {
          "number_of_fragments" : 0
        },
        "name.pinyin" : {
          "number_of_fragments" : 0
        },
        "address" : {
          "number_of_fragments" : 0
        }
      }
    }
}

比如有一个场馆名字叫测试场馆,那么你输入“cs”搜索的时候也能搜索到该场馆。并且能将场馆名高亮显示,区别就是输入的是首拼的话,高亮值在name.pinyin上,如果输入的是汉字的话,高亮值在name上,name和name.pinyin高亮输出的内容是一样的。
{
   "took": 5,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": null,
      "hits": [
         {
            "_index": "huiti_app_v1",
            "_type": "stadium",
            "_id": "id1",
            "_score": null,
            "_source": {
               "createTime": "2015-01-01 00:00:00"
            },
            "highlight": {
               "name.pinyin": [
                  "<font color='red'>测试</font>场馆1"
               ]
            },
            "sort": [
               1420070400000
            ]
         },
         {
            "_index": "huiti_app_v1",
            "_type": "stadium",
            "_id": "id2",
            "_score": null,
            "_source": {
               "createTime": "2015-01-01 00:00:00"
            },
            "highlight": {
               "name.pinyin": [
                  "<font color='red'>测试</font>场馆2"
               ]
            },
            "sort": [
               1420070400000
            ]
         },
         {
            "_index": "huiti_app_v1",
            "_type": "stadium",
            "_id": "id3",
            "_score": null,
            "_source": {
               "createTime": "2015-01-01 00:00:00"
            },
            "highlight": {
               "name.pinyin": [
                  "<font color='red'>测试</font>场馆3"
               ]
            },
            "sort": [
               1420070400000
            ]
         }
      ]
   }
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值