3-Elasticsearch深入搜索-近似匹配

一起来玩Elasticsearch,加我微信:wx1250134974

Elasticsearch认证复习准备

https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html

##近似匹配概念:

举例说明:对于“eat food” ,不仅仅能区分是否包含eat或者food这一层,还能区分eat food这个关系(这个关系也仅仅是分词之间临近)。可以用这个功能做自动机器人应答,把历史的问与答存储好,通过检索更符合的问题,找到历史应答。

 

 

1、近似匹配

GET /my_index/my_type/_search

{

    "query": {

        "match_phrase": {

            "title": "quick brown fox"

        }

    }

}

注:所有的term必须都出现,并且位置顺序也必须一样

 

 

GET /my_index/my_type/_search

{

    "query": {

        "match_phrase": {

            "title": {

             "query": "quick fox",

             "slop":  1

            }

        }

    }

}

注:所有term必须全部出现,位置约束可以放宽一些(slop 控制位置约束严格的灵活度)

 

 

 

 

 

 

 

 

GET /my_index/my_type/_search

{

  "query": {

    "bool": {

      "must": {

        "match": {

          "title": {

            "query":                "quick brown fox",

            "minimum_should_match": "30%"

          }

        }

      },

      "should": {

        "match_phrase": {

          "title": {

            "query": "quick brown fox",

            "slop":  50

          }

        }

      }

    }

  }

}

注:使用邻近度来提高相关度,这样可以返回较多的结果,且邻近度高的会排在前面。

 

 

GET /my_index/my_type/_search

{

    "query": {

        "match": {  

            "title": {

                "query":                "quick brown fox",

                "minimum_should_match": "30%"

            }

        }

    },

    "rescore": {

        "window_size": 50,

        "query": {         

            "rescore_query": {

                "match_phrase": {

                    "title": {

                        "query": "quick brown fox",

                        "slop":  50

                    }

                }

            }

        }

    }

}

注:

match 查询决定哪些文档将包含在最终结果集中,并通过 TF/IDF 排序。

window_size 是每一分片进行重新评分的顶部文档数量。

 

 

 

  1. 在索引时生成相关词term,这样关系就存在于term中,有助于更精确匹配合适文档。这种操作性能还是不错的,唯一牺牲的可能就是多占用磁盘了

PUT /my_index2

{

    "settings": {

        "number_of_shards": 1,  

        "analysis": {

            "filter": {

                "my_shingle_filter": {

                    "type":             "shingle",

                    "min_shingle_size": 2,

                    "max_shingle_size": 2,

                    "output_unigrams":  false   

                }

            },

            "analyzer": {

                "my_shingle_analyzer": {

                    "type":             "custom",

                    "tokenizer":        "standard",

                    "filter": [

                        "lowercase",

                        "my_shingle_filter"

                    ]

                }

            }

        }

    }

}

注:

 

 

A、"min_shingle_size"和"max_shingle_size" 默认最小/最大的 shingle 大小是 2 ,所以实际上不需要设置。

B、"output_unigrams" shingle 语汇单元过滤器默认输出 unigrams ,但是我们想让 unigrams 和 bigrams 分开。

C、my_shingle_analyzer 使用我们常规的 my_shingles_filter 语汇单元过滤器。

 

 

GET /my_index2/_analyze?analyzer=my_shingle_analyzer

{

  "text":"Sue ate the alligator"

}

注:看下分词效果,使用的话就直接用即可,最好把这个分词的效果单独存放使用。

 

 

 

 

一起来玩Elasticsearch,加我微信:wx1250134974

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值