栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch 的基本查询语法使用(中)

elasticsearch 的基本查询语法使用(中)

1.Full-text queries:全文检索 

        (1)query-term:不会被分词

        注意:前一篇写的用的是match,那个是可以被分词的 term对应的是精确查找,这里有可能有些不理解,首先name :"Air iphone" ,Air iphone这个字段,在我进行保存的时候就已经进行了分词,所以他不能整个拿出来。

GET /zzl/_search
{
  "query": {
    "term": {
      "name": {
        "value": "Air iphone"
      }
    }
  }
}

        查询结果:

#这里不会展示出你想要的东西
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

#另外可以用分词器分析一下这个单词(分词器会有专门的文章来说明)
GET /_analyze
{
  "analyzer": "standard",
  "text": ["Air iphone"]
}

#查询结果
{
  "tokens" : [
    {
      "token" : "air",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "iphone",
      "start_offset" : 4,
      "end_offset" : 10,
      "type" : "",
      "position" : 1
    }
  ]
}
可以看到 Air iphone 被分成了两个单词

        match 和term的区别

                match查询的字段,会被分词,这个和你设置的分词器相关

                term 不会被分词

        (2)Phrase search短语搜索

         和全文检索相反,他是以一个短语进行查询

GET /zzl/_search
{
  "query": {
    "match_phrase": {
      "name": "Air iphone"
    }
  }
}

        查询结果:

2.多条件查询

        (1)Query and filter:查询和过滤

        多条件查询需要用到bool这个关键字,bool是采用的more_matches_is_better机制,因此满足must和should子句的文档将会合并起来计算分值。

                must:必须满足

                子句(查询)必须出现在匹配的文档中,并将有助于得分。

                filter:过滤器 不计算相关度分数,cache☆

                子句(查询)必须出现在匹配的文档中。但是不像 must查询的分数将被忽略。Filter子句在filter上下文中执行,这意味着计分被忽略,并且子句被考虑用于缓存。

                should:可能满足 or

                子句(查询)应出现在匹配的文档中。

​​​​​​​        ​​​​​​​        ​​​​​​​must_not:必须不满足 不计算相关度分数   not                

                子句(查询)不得出现在匹配的文档中。子句在过滤器上下文中执行,这意味着计分被忽略,并且子句被视为用于缓存。由于忽略计分,0因此将返回所有文档的分数。

​​​​​​​        ​​​​​​​        minimum_should_match

                最少需要多少条件成立

        简单示例

GET /zzl/_search
{
  "_source": ["name","price"], 
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "iphone"
        } 
        }
      ],
      "filter": [
        {"range": {
          "price": {
            "gte":50
          }
        }}
      ]
    }
  }
}


结果展示
 "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.1505529,
    "hits" : [
      {
        "_index" : "zzl",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.1505529,
        "_source" : {
          "price" : 9999,
          "name" : "iphone"
        }
      },
      {
        "_index" : "zzl",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.1505529,
        "_source" : {
          "price" : 9999,
          "name" : "iphone"
        }
      },
      {
        "_index" : "zzl",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 0.92834306,
        "_source" : {
          "price" : 9999,
          "name" : "Air iphone"
        }
      },
      {
        "_index" : "zzl",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.77807164,
        "_source" : {
          "price" : 6999,
          "name" : "iphone nfc phone"
        }
      }
    ]
  }

剩下的就随缘更新这一块了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号