zoukankan      html  css  js  c++  java
  • 【ES】学习5-全文搜索

    全文搜索两个最重要的方面是:相关性, 分析。

    一旦谈论相关性或分析这两个方面的问题时,我们所处的语境是关于查询的而不是过滤

    match:单个词查询

    GET /my_index/my_type/_search
    {
        "query": {
            "match": {
                "title": "QUICK!"
            }
        }
    }

    步骤:1.分析字段类型,是string。2.分析查询字符串,得到一个单词quick。3.用term查询,在倒排索引中查找quick。4.为文档评分。

    match:多个词查询

    GET /my_index/my_type/_search
    {
        "query": {
            "match": {
                "title": "BROWN DOG!"
            }
        }
    }

     match 查询必须查找两个词( ["brown","dog"] ),它在内部实际上先执行两次 term 查询,然后将两次查询的结果合并作为最终结果输出。

    上面的查询,哪怕只有一个brown或者是一个dog也会匹配。

    如果要求一定要同时出现brown和dog两个词,则需要operator操作符。

    GET /my_index/my_type/_search
    {
        "query": {
            "match": {
                "title": {      
                    "query":    "BROWN DOG!",
                    "operator": "and"      #and表示两个必须都匹配,or表示匹配一个即可
                }
            }
        }
    }

    用minimum_should_match指定必须匹配的词项数。可以是百分比。

    GET /my_index/my_type/_search
    {
      "query": {
        "match": {
          "title": {
            "query":                "quick brown dog",
            "minimum_should_match": "75%"
          }
        }
      }
    }

    组合查询:

    GET /my_index/my_type/_search
    {
      "query": {
        "bool": {
          "must":     { "match": { "title": "quick" }},
          "must_not": { "match": { "title": "lazy"  }},
          "should": [
                      { "match": { "title": "brown" }},
                      { "match": { "title": "dog"   }}
          ]
        }
      }
    }

    should语句的条件不是必须满足的,但是如果满足,则会增加文档的分数,即认为其更相关。

    可以用minimum_should_match限制必须满足should的条件数。

    GET /my_index/my_type/_search
    {
      "query": {
        "bool": {
          "should": [
            { "match": { "title": "brown" }},
            { "match": { "title": "fox"   }},
            { "match": { "title": "dog"   }}
          ],
          "minimum_should_match": 2 
        }
      }
    }

    下面的两个查询是等价的:

    {
        "match": {
            "title": {
                "query":                "quick brown fox",
                "minimum_should_match": "75%"
            }
        }
    }
    {
      "bool": {
        "should": [
          { "term": { "title": "brown" }},
          { "term": { "title": "fox"   }},
          { "term": { "title": "quick" }}
        ],
        "minimum_should_match": 2 
      }
    }

    下面两个查询等价:

    {
        "match": {
            "title": {
                "query":    "brown fox",
                "operator": "and"
            }
        }
    }
    {
      "bool": {
        "must": [
          { "term": { "title": "brown" }},
          { "term": { "title": "fox"   }}
        ]
      }
    }

    用boost参数提高should条件的重要性。

    我们想让包含 Lucene 的有更高的权重,并且包含 Elasticsearch 的语句比 Lucene 的权重更高

    GET /_search
    {
        "query": {
            "bool": {
                "must": {
                    "match": {  
                        "content": {
                            "query":    "full text search",
                            "operator": "and"
                        }
                    }
                },
                "should": [
                    { "match": {
                        "content": {
                            "query": "Elasticsearch",
                            "boost": 3 
                        }
                    }},
                    { "match": {
                        "content": {
                            "query": "Lucene",
                            "boost": 2 
                        }
                    }}
                ]
            }
        }
    }

    boost 参数被用来提升一个语句的相对权重( boost 值大于 1 )或降低相对权重( boost值处于 0 到 1 之间)

    原文后面还介绍了分析器和数据过少时的相关度问题。

  • 相关阅读:
    laravel-13-笔记-1
    laravel-14-笔记-2
    supervisor监听器-linux安装配置
    laravel-12-artisan命令创建view文件
    linux修改主机名
    laravel-11-laravel 模型Eloquent ORM
    laravel-composer安装laravel
    laravel-10-laravel collection集合
    laravel-8-laravel数据填充
    laravel-9-laravel数据库查询 查询组件
  • 原文地址:https://www.cnblogs.com/dplearning/p/6994424.html
Copyright © 2011-2022 走看看