第12篇-Elasticsearch全文查询

个人Elasticsearch系列文章,逐渐更新中,欢迎关注
0A.关于Elasticsearch及实例应用
00.Solr与ElasticSearch对比
01.ElasticSearch能作什么?
02.Elastic Stack功能介绍
03.如何安装与设置Elasticsearch API
04.若是经过elasticsearch的head插件创建索引_CRUD操做
05.Elasticsearch多个实例和head plugin使用介绍
06.当Elasticsearch进行文档索引时,它是如何工做的?
07.Elasticsearch中的映射方式—简洁版教程
08.Elasticsearch中的分析和分析器应用方式
09.Elasticsearch中构建自定义分析器
10.Kibana科普-做为Elasticsearhc开发工具
11.Elasticsearch查询方法
12.Elasticsearch全文查询php

另外Elasticsearch入门,我强烈推荐ElasticSearch搭建小白指南给你,很是想尽的入门指南手册。html

咱们已经学习了Elasticsearch查询的基本分类,这两个类别的基本知识以及查询/过滤器上下文。在此博客中,其目的是向您介绍Elasticsearch世界中常见的全文查询。
让咱们索引一些主要由一些文本组成的数据。为简单起见,我采用了Facebook帖子的修剪版本及其说明和详细信息的CSV,这些内容能够在公共网站上得到。您能够将这些tweet索引到Elasticsearch
我已将上述推文索引到名为fb-post的索引。索引后的样本数据文档以下所示:segmentfault

{
        "_index" : "fb-post",
        "_type" : "_doc",
        "_id" : "TszxwG0Bm6hFGbtHjVCC",
        "_score" : 1.0,
        "_source" : {
          "status_type" : "shared_story",
          "link" : "http://abcnews.go.com/blogs/headlines/2011/12/chief-justice-roberts-responds-to-judicial-ethics-critics/",
          "description" : "PAUL J. RICHARDS/AFP/Getty Images Chief Justice John Roberts issued a ringing endorsement Saturday night of his colleagues’ ability to determine when they should step down from a case because of a conflict of interest. “I have complete confidence in the capability of my colleagues to determine when ...",
          "caption" : "abcnews.go.com",
          "love_count" : 0,
          "shares_count" : 12,
          "page_id" : 86680728811,
          "wow_count" : 0,
          "post_type" : "link",
          "id" : "86680728811_272953252761568",
          "posted_at" : "2012-01-01 00:30:26",
          "sad_count" : 0,
          "angry_count" : 0,
          "message" : "Roberts took the unusual step of devoting the majority of  his annual  report to the issue of judicial ethics.",
          "picture" : "https://external.xx.fbcdn.net/safe_image.php?d=AQAPXteeHLT2K7Rb&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FPolitics%2Fgty_chief_justice_john_roberts_jt_111231_wblog.jpg&cfs=1&sx=108&sy=0&sw=269&sh=269",
          "likes_count" : 61,
          "thankful_count" : 0,
          "@timestamp" : "2012-01-01T00:30:26.000+05:30",
          "comments_count" : 27,
          "name" : "Chief Justice Roberts Responds to Judicial Ethics Critics",
          "haha_count" : 0
        }
      }

在上面的文档中,咱们感兴趣的字段是诸如“名称”,“消息”和“描述”之类的文本字段。
如今让咱们一个接一个地转到每一个全文查询。
1.匹配查询
咱们在以前的博客中讨论了匹配查询,可是没有提到匹配查询的正经常使用例。匹配查询最多见的用例是当咱们拥有大量数据集时,咱们须要快速找到一些近似精确的匹配项。
例如,在咱们的Twitter数据集中,咱们须要肯定整个推文集中是否存在“信心”一词。可使用针对如下“文本”字段的简单匹配查询来完成此操做:elasticsearch

POST fb-post/_search
{
  "query": {
    "match": {
      "description": {
        "query":"confidence"
      }
    }
  }
}

结果将显示带有“ confidence”文本的推文。
如今在上面的示例中,咱们只看到了一个单词。当咱们输入多个单词时会发生什么?让咱们尝试下面的查询,这里咱们要给出的查询是“ 信心大厦 ”ide

POST fb-post/_search
{
  "query": {
    "match": {
      "description": {
        "query":"confidence buildings"
      }
    }
  }
}

如今,这将返回匹配“信心” 或 “建筑物”的文档。匹配查询的默认行为为OR。这能够更改。若是咱们要同时匹配“信心” 和“建筑物”,则能够在查询中指定“ operator”参数,以下所示:
POST fb-post/_search工具

{
  "query": {
    "match": {
      "description": {
        "query":"confidence buildings",
        "operator":"AND"
      }
    }
  }
}

上面的查询将返回包含“信心”和“建筑物”(在咱们的数据集中为零)的文档
2.多重比对查询
顾名思义,多匹配查询将在多个字段中搜索搜索关键字。假设咱们有一个搜索关键字“ Giffords family”,能够在“名称”和“描述”字段中进行搜索,则可使用多重匹配查询。post

POST fb-post/_search
{
  "query": {
    "multi_match" : {
      "query":    "Giffords family", 
      "fields": [ "name", "description" ] 
    }
  }
}

在此处,针对“名称”和“描述”字段搜索“ Giffords”或“ family”一词,并返回匹配的文档。
咱们还能够针对特定字段进行自定义评分。在下面的查询中,对全部与“名称”字段中的关键字匹配的文档给予5的提高学习

POST fb-post/_search
{
  "query": {
    "multi_match" : {
      "query":    "Giffords family", 
      "fields": [ "name^5", "description" ] 
    }
  }
}
  1. query_string查询

另外一个有用的查询是query_string查询。它与匹配查询相似,但此处搜索关键字的格式很重要。它须要特定的格式,而且若是搜索关键字的格式不一样,则会返回错误。
考虑如下查询:开发工具

POST fb-post/_search
{
    "query": {
        "query_string" : {
            "query" : "(step down) OR (official act)"
        }
    }
}

在此,搜索关键字首先分为两部分,即“或”条件的左侧和“或”条件的右侧。也就是说,搜索查询中的运算符用做定界符。而后将对每一个部分进行分析(根据要查询的字段,在上面的示例中查询全部字段,它将进行标准分析),而后进行查询。
也能够对特定的一个或多个字段进行查询,以下所示:网站

POST fb-post/_search
{
    "query": {
        "query_string" : {
            "query" : "(step down) OR (official act)",
            "fields" : ["description","name"]
        }
    }
}
  1. match_phrase查询

Match_phrase查询是一个特别有用的查询,它寻找匹配短语而不是单个单词。在下面给出的示例中,match_phrase查询以相同顺序获取与单词“ deeply关心”匹配的文档。

POST fb-post / _search 
{ 
    “ query”:{ 
        “ match_phrase”:{ 
            “ description”:“ 密切关注 ” 
        } 
    } 
}

即便更改了单词顺序,match_phrase查询的一个很是有用的自定义设置也会匹配。例如,若是咱们但愿“深切关注”和“深切关注”相匹配,则能够将slop参数与match_phrase查询一块儿使用,以下所示:

POST fb-post/_search
{
    "query": {
        "match_phrase" : {
            "description" : "deeply concerned"
        }
    }
}

slope值默认为0,最大范围为50。在上面的示例中,slope值2表示能够将这些词视为匹配项的范围。
如今考虑如下查询,在该查询的末尾加上不完整的关键字“ ab”。该match_phrase查询没有提供火柴,即便存在具备“深切关注文档此查询有关 ” 短语中的“描述”字段

POST fb-post/_search
{
    "query": {
        "match_phrase": {
            "description" : {
                "query" : "deeply concerned",
                "slop": 2
            }
        }
    }
}
  1. match_phrase_prefix查询

在上面的示例中,咱们看到match_phrase查询须要精确的短语来进行匹配。可是有时候,若是咱们也可使用match_phrase_prefix查询来匹​​配部分匹配项,那将很方便。“ match_phrase_prefix”查询可帮助咱们实现此类匹配。

POST fb-post/_search
{
    "query": {
        "match_phrase" : {
            "description" : "deeply concerned ab"
        }
    }
}

上面的查询能够像下面搭配词组:
“deeply concerned about”
“deeply concerned above”
一个实际的用例是邮政编码的自动完成实现,其中用户键入部分短语。

结论在此博客中,咱们看到了Elasticsearch查询世界中的一些重要的全文查询。我将在下一个博客中介绍术语级别查询,而后再返回一些特殊的全文查询,这将有助于更好地理解。

相关文章
相关标签/搜索