美文网首页
ES 全文搜索

ES 全文搜索

作者: 王某某的笔记 | 来源:发表于2017-04-25 10:32 被阅读651次

ES 全文搜索

  1. 全文搜索
GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": "BROWN DOG!"
        }
    }
}

使用了match查询的多词查询只是简单地将生成的term查询包含在了一个bool查询中。通过默认的or操作符,每个term查询都以一个语句被添加,所以至少一个should语句需要被匹配。以下两个查询是等价的:

{
    "match": { "title": "brown fox"}
}
{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}
  1. 提高查询精度
    match查询接受一个operator参数,该参数的默认值是"or"。你可以将它改变为"and"来要求所有的词条都需要被匹配:
GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": {      
                "query":    "BROWN DOG!",
                "operator": "and"
            }
        }
    }
}

使用and操作符时,所有的term查询都以must语句被添加,因此所有的查询都需要匹配。以下两个查询是等价的:

{
    "match": {
        "title": {
            "query":    "brown fox",
            "operator": "and"
        }
    }
}
{
  "bool": {
    "must": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}
  1. 控制查询精度
    match查询支持minimum_should_match参数,它能够让你指定有多少词条必须被匹配才会让该文档被当做一个相关的文档。尽管你能够指定一个词条的绝对数量,但是通常指定一个百分比会更有意义,因为你无法控制用户会输入多少个词条:
GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title": {
        "query":                "quick brown dog",
        "minimum_should_match": "75%"
      }
    }
  }
}

如果指定了minimum_should_match参数,它会直接被传入到bool查询中,因此下面两个查询是等价的:

{
    "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 
  }
}
  1. 合并查询
    bool查询通过must,must_not以及should参数来接受多个查询。比如:
GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "must":     { "match": { "title": "quick" }},
      "must_not": { "match": { "title": "lazy"  }},
      "should": [
                  { "match": { "title": "brown" }},
                  { "match": { "title": "dog"   }}
      ]
    }
  }
}
  1. 控制精度
    正如可以控制match查询的精度,也能够通过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 
    }
  }
}
  1. 提升查询子句(Boosting Query Clause)
    假设我们需要搜索和"full-text search"相关的文档,但是我们想要给予那些提到了"Elasticsearch"或者"Lucene"的文档更多权重。更多权重的意思是,对于提到了"Elasticsearch"或者"Lucene"的文档,它们的相关度_score会更高,即它们会出现在结果列表的前面。
    一个简单的bool查询能够让我们表达较为复杂的逻辑:
GET /_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "content": { 
                        "query":    "full text search",
                        "operator": "and"
                    }
                }
            },
            "should": [ 
                { "match": { "content": "Elasticsearch" }},
                { "match": { "content": "Lucene"        }}
            ]
        }
    }
}
  1. 多个查询字符串(Multiple Query Strings)
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "War and Peace" }},
        { "match": { "author": "Leo Tolstoy"   }}
      ]
    }
  }
}
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "War and Peace" }},
        { "match": { "author": "Leo Tolstoy"   }},
        { "bool":  {
          "should": [
            { "match": { "translator": "Constance Garnett" }},
            { "match": { "translator": "Louise Maude"      }}
          ]
        }}
      ]
    }
  }
}
  1. 设置子句优先级
    通过boost参数,增加字段的权重
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { 
            "title":  {
              "query": "War and Peace",
              "boost": 2
        }}},
        { "match": { 
            "author":  {
              "query": "Leo Tolstoy",
              "boost": 2
        }}},
        { "bool":  { 
            "should": [
              { "match": { "translator": "Constance Garnett" }},
              { "match": { "translator": "Louise Maude"      }}
            ]
        }}
      ]
    }
  }
}
  1. 多字段查询
{
    "multi_match": {
        "query":                "Quick brown fox",
        "type":                 "best_fields", 
        "fields":               [ "title", "body" ],
        "tie_breaker":          0.3,
        "minimum_should_match": "30%" 
    }
}

注意到以上的type属性为best_fields、minimum_should_match和operator参数会被传入到生成的match查询中。
在字段名中使用通配符

{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": "*_title"
    }
}

提升个别字段
个别字段可以通过caret语法()进行提升:仅需要在字段名后添加boost,其中的boost是一个浮点数:

{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": [ "*_title", "chapter_title^2" ] 
    }
}

同一个字段使用了不同的解析器的情况

GET /my_index/_search
{
   "query": {
        "multi_match": {
            "query":  "jumping rabbits",
            "type":   "most_fields", 
            "fields": [ "title", "title.std" ]
        }
    }
}

相关文章

  • ES 全文搜索

    ES 全文搜索 全文搜索 使用了match查询的多词查询只是简单地将生成的term查询包含在了一个bool查询中。...

  • ES基础-原理篇

    ES - 基础 ES简介篇 ES介绍 ElasticSearch是一种分布式全文搜索引擎,基于Lucene(全文搜...

  • ElasticSearch

    ES介绍 ES开源 可扩展 的 分布式 全文搜索和数据分析引擎 功能 分布式搜索引擎 全文检索 数据分析引擎 对海...

  • 几个搜索的相关话题

    结构化搜索与全文搜索 ES在搜索时有两种类型,即全文搜索与结构化搜索。其相对应于"term"系列的查询 和 "ma...

  • Elasticsearch5.5.1基本安装及相关插件

    最近项目用到了es搜索引擎,行业对全文搜索引擎方面对es具有高度的评价,es基于目前最流行的开源Luence封装的...

  • elasticsearch学习系列一(ES背景及简介)

    ES简介 1. ES是什么? Elasticsearch 是一个开源的搜索引擎,建立在全文搜索引擎库 Apache...

  • ElasticSearch | 用 JDBC 插件导入数据到 E

    需求 将数据库中的数据同步到 ES,借助 ES 的全文搜索,提高搜索的速度。 需要把新增用户信息同步到 Elast...

  • ES学习教程

    前言 es是什么? es是基于Apache Lucene的开源分布式(全文)搜索引擎,,提供简单的RESTful ...

  • (九)同步数据库数据到ES中

    1、需求描述 将数据库中数据同步到ES中,借助ES全文检索,提高搜索效率 用户update之后,需要同步到ES; ...

  • ES学习教程

    前言 es是什么?es是基于Apache Lucene的开源分布式(全文)搜索引擎,,提供简单的RESTful A...

网友评论

      本文标题:ES 全文搜索

      本文链接:https://www.haomeiwen.com/subject/ictvzttx.html