基于elasticsearch7.6.1 和 kibana7.6.1

本文通过案例进行讲解,希望读者耐心阅读【3.查询】中的内容。

1. 创建索引

PUT goods
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}

 说明:

  1. 通常情况下,为了提升搜索的效果,ik_max_word和ik_smart两种分词器需要配合使用。
  2. 即构建索引时用ik_max_word,尽可能多的分词,而搜索时用ik_smart,尽可能提高匹配准度,让用户的搜索尽可能准确。

2. 通过_bulk批量导入数据

POST goods/_bulk
{"index":{"_id":1}}
{"title":"法国原瓶进口红酒"}
{"index":{"_id":2}}
{"title":"圣罗兰山茶色口红"}
{"index":{"_id":3}}
{"title":"康师傅红烧牛肉味方便面"}
{"index":{"_id":4}}
{"title":"康师傅芥末青柠味方便面"}
{"index":{"_id":5}}
{"title":"康师傅香辣牛肉味方便面"}
{"index":{"_id":6}}
{"title":"康师傅极致酷爽红茶"}
{"index":{"_id":7}}
{"title":"新西兰进口牛奶"}

3. 查询

# match查询
# 基于"进口" OR "红酒",进行召回。
GET goods/_search
{
  "query": {
    "match": {
      "title": "进口红酒"
    }
  }
}


GET goods/_search
{
  "query": {
    "match": {
      "title": "口红"
    }
  }
}


# 基于"康师傅" OR "红烧",进行召回。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧"
      }
    }
  }
}

# 基于"康师傅" OR "红烧",进行召回,但是必须匹配两个词以上。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧",
        "operator": "or",
        "minimum_should_match": 2
      }
    }
  }
}

# 基于"康师傅" AND "红烧",进行召回。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧",
        "operator": "and"
      }
    }
  }
}

# 基于"康师傅" OR "红烧" OR "方便面",进行召回,但是必须匹配两个词以上。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧方便面",
        "operator": "or",
        "minimum_should_match": 2
      }
    }
  }
}

# 3*75%=2.25,向下取整等于2。
# 基于"康师傅" OR "红烧" OR "方便面",进行召回,但是必须匹配两个词以上。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧方便面",
        "operator": "or",
        "minimum_should_match": "75%"
      }
    }
  }
}

# 等价写法
GET goods/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": "康师傅"
          }
        },
        {
          "term": {
            "title": "红烧"
          }
        },
        {
          "term": {
            "title": "方便面"
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}


# 3*60%=1.8,向下取整等于1。
# 基于"康师傅" OR "红烧" OR "方便面",进行召回,但是必须匹配一个词以上。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧方便面",
        "operator": "or",
        "minimum_should_match": "60%"
      }
    }
  }
}

# 等价写法
GET goods/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": "康师傅"
          }
        },
        {
          "term": {
            "title": "红烧"
          }
        },
        {
          "term": {
            "title": "方便面"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}


# 如果clauses<=3,那么全部should条件都要满足,如果clauses>3,那么需要满足全部should条件的80%。
# 5*80%=4,向下取整等于4。
# 基于"康师傅" OR "红烧" OR "牛肉" OR "味" OR "方便面",进行召回,但是必须匹配4个词以上。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧牛肉味方便面",
        "operator": "or",
        "minimum_should_match": "3<80%"
      }
    }
  }
}

# 等价写法
GET goods/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": "康师傅"
          }
        },
        {
          "term": {
            "title": "红烧"
          }
        },
        {
          "term": {
            "title": "牛肉"
          }
        },
        {
          "term": {
            "title": "味"
          }
        },
        {
          "term": {
            "title": "方便面"
          }
        }
      ],
      "minimum_should_match": 4
    }
  }
}


# 如果clauses<=3,那么全部should条件都要满足,如果clauses>3,那么需要满足全部should条件的80%。
# 基于"康师傅" OR "红烧" OR "方便面",进行召回,但是必须匹配3个词以上。
GET goods/_search
{
  "query": {
    "match": {
      "title": {
        "query": "康师傅红烧方便面",
        "operator": "or",
        "minimum_should_match": "3<80%"
      }
    }
  }
}

# 等价写法
GET goods/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": "康师傅"
          }
        },
        {
          "term": {
            "title": "红烧"
          }
        },
        {
          "term": {
            "title": "方便面"
          }
        }
      ],
      "minimum_should_match": 3
    }
  }
}

  说明:

  1. "minimum_should_match": "3<80%"含义:当should分支总数小于等于3时,则必须匹配所有的should分支,当should分支总数大于3时,则至少匹配80%的should分支,同时分支数向下取整。
  2. "minimum_should_match": "60%"含义:必须匹配should分支总数的60%,同时分支数向下取整。例如,总共有7个should分支,则7*0.6=4.2,向下取整得到4,即至少匹配4个should分支。

4. 补充,对比ik_max_word和ik_smart的分词效果

GET _analyze
{
  "analyzer": "ik_smart",
  "text": "进口红酒"
}


GET _analyze
{
  "analyzer": "ik_smart",
  "text": "康师傅红烧"
}


GET _analyze
{
  "analyzer": "ik_smart",
  "text": "康师傅红烧方便面"
}


GET _analyze
{
  "analyzer": "ik_smart",
  "text": "康师傅红烧牛肉味方便面"
}


GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "新西兰进口牛奶"
}


GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "康师傅红烧牛肉味方便面"
}


GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "康师傅香辣牛肉味方便面"
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐