elasticsearch自定义分析器

2019-08-06  本文已影响0人  陈文瑜

elasticsearch分析器三功能自定义

自定义分析器

实例

#设置setting
PUT /enterpriseextend
{
  "settings": {
    "analysis": {
      "filter": {
        "edge_ngram_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 50
        },
        "pinyin_simple_filter": {
          "type": "pinyin",
          "keep_first_letter": true,
          "keep_separate_first_letter": false,
          "keep_full_pinyin": false,
          "keep_original": false,
          "limit_first_letter_length": 50,
          "lowercase": true
        },
        "pinyin_full_filter": {
          "type": "pinyin",
          "keep_first_letter": false,
          "keep_separate_first_letter": false,
          "keep_full_pinyin": true,
          "none_chinese_pinyin_tokenize": true,
          "keep_original": false,
          "limit_first_letter_length": 50,
          "lowercase": true
        }
      },
      "tokenizer": {
        "ik_max_word": {
          "type": "ik_max_word",
          "use_smart": true
        }
      },
      "analyzer": {
        "ngramIndexAnalyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "edge_ngram_filter",
            "lowercase"
          ]
        },
        "ikIndexAnalyzer": {
          "type": "custom",
          "tokenizer": "ik_max_word"
        },
        "pinyiSimpleIndexAnalyzer": {
          "tokenizer": "keyword",
          "filter": [
            "pinyin_simple_filter",
            "edge_ngram_filter",
            "lowercase"
          ]
        },
        "pinyiFullIndexAnalyzer": {
          "tokenizer": "keyword",
          "filter": [
            "pinyin_full_filter",
            "lowercase"
          ]
        }
      }
    }
  }
}
#设置mapping
PUT enterpriseextend/_mapping/enterpriseextend
{
  "properties": {
    "id": {
      "type": "long"
    },
    "entName": {
      "type": "text", 
      "analyzer": "ikIndexAnalyzer",
      "fields": {
        "ngram": {
          "type": "text", 
          "analyzer": "ngramIndexAnalyzer"
        },
        "SPY": {
          "type": "text", 
          "analyzer": "pinyiSimpleIndexAnalyzer"
        },
        "FPY": {
          "type": "text", 
          "analyzer": "pinyiFullIndexAnalyzer"
        }
      }
    }
  }
}
#插入语句
PUT enterpriseextend/_doc/1
{
  "entName":"确实不是啥好东西"
}
#三种查询
GET enterpriseextend/_search
{
  "query": {
    "match": {
      "entName.ngram": "确实不是啥好东西"
    }
  }
}
GET enterpriseextend/_search
{
  "query": {
    "match": {
      "entName.SPY": "qsbsshdx"
    }
  }
}
GET enterpriseextend/_search
{
  "query": {
    "match": {
      "entName.FPY": "queshibushishahaodongxi"
    }
  }
}

综合查询

GET enterpriseextend/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "entName.ngram": {
              "query": "确实不是啥好东西",
              "boost": 5 
            }
          }
        },
        {
          "match": { 
            "entName.SPY": {
              "query": "qsbsshdx",
              "boost": 1 
            }
          }
        },
        {
          "match": { 
            "entName.FPY": {
              "query": "queshibushishahaodongxi",
              "boost": 0.8
            }
          }
        }
      ]
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读