1. 程式人生 > 其它 >elasticsearch查詢匹配度高的排序

elasticsearch查詢匹配度高的排序

技術標籤:elasticsearch

核心:分詞查詢和非分詞都可查詢到

{"query_string": {
"query": "*地* 地 英國 *英國*",
"fields": ["name*"],
"type": "most_fields"
}

PUT /test1
{

  "mappings": {
    "test1": {
      "dynamic_templates": [
        {
          "notanalyzed": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_smart",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}

GET /test1/_search

GET /test1/_mapping

PUT /test1/test1/niu010
{
  "id":"0010",
  "name1":"中國的美酒咖啡飲料",
  "name2":"日本地方",
  "name3":"英國地方",
  "name4":"美國廣電"
}

PUT /test1/test1/niu020
{
  "id":"0020",
  "name1":"中國的美酒咖啡飲料",
  "name2":"日本的咖啡飲料",
  "name3":"得國的飲料",
  "name4":"義大利的美酒咖啡飲料"
}

PUT /test1/test1/niu030
{
  "id":"0030",
  "name1":"中國的美酒咖啡飲料"
}

PUT /test1/test1/niu040
{
  "id":"0040",
  "name1":"中國的飲料",
  "name3":"英國的美酒"
}

PUT /test1/test1/niu050
{
  "id":"0050",
  "name1":"中國的瘋子",
  "name2":"日本的二百"
}

PUT /test1/test1/niu060
{
  "id":"0060",
  "name1":"中國的飲料",
  "name2":"日本的美酒",
  "name3":"英國的可樂"
}

PUT /test1/test1/niu070
{
  "id":"0070",
  "name2":"日本是對方的"
}

GET /test1/_search
{
  "query": {
    "bool": {
      "must": [
        {"query_string": {
          "query": "*地* 地 英國 *英國*",
          "fields": ["name*"],
          "type": "most_fields"
        }}
      ]
    }
  }
}