Elasticsearch 检索相关

1、 检索所有文档

GET bus/product/_search

2、 term检索

term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词,所以我们的搜索词必须是文档分词集合中的一个,如果没有安装分词插件,汉字分词按每个汉字来分。

查询不到内容:
GET bus/product/_search
{
  "query": {
    "term": {
        "producer": "公交"
    }
  }
}
producer中所有带“公”的文档都会被查询出来
GET bus/product/_search
{
  "query": {
    "term": {
        "producer": "公"
    }
  }
}

3、 match检索

match查询会先对搜索词进行分词,分词完毕后再逐个对分词结果进行匹配,因此相比于term的精确搜索,match是分词匹配搜索

描述中带有机场酒店四个字的各种组合的文档都会被返回
GET bus/product/_search
{
  "query": {
    "match": {
      "desc": "机场酒店"
    }
  }
}

4、 分页

GET bus/_search
{
  "from": 0,
  "size": 3,
  "query": {
    "match": {
      "desc": "机场酒店"
    }
  }
}

GET bus/_search{  "from": 0,   "size": 5,   "query": {    "match_all": {}  }}

5、 过滤字段,类似select a,b from table中a,b

GET bus/_search
{
  "_source": ["name","desc"] ,
  "query": {
    "match": {
      "desc": "机场"
    }
  }
}

result:{  "took" : 14,  "timed_out" : false,  "_shards" : {    "total" : 3,    "successful" : 3,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : 12,    "max_score" : 2.1208954,    "hits" : [      {        "_index" : "bus",        "_type" : "product",        "_id" : "9",        "_score" : 2.1208954,        "_source" : {          "name" : "机场大巴A2线",          "desc" : "机机场场"        }      },      {        "_index" : "bus",        "_type" : "product",        "_id" : "10",        "_score" : 2.1208954,        "_source" : {          "name" : "机场大巴A2线",          "desc" : "机机场场"        }      },      {        "_index" : "bus",        "_type" : "product",        "_id" : "6",        "_score" : 0.62362677,        "_source" : {          "name" : "机场大巴A2线",          "desc" : "机机场场"        }      }    ]  }}

6、 检索结果显示版本

GET bus/_search
{
  "version": true,
  "from": 0,
  "size": 3,
  "query": {
    "match": {
      "desc": "机场酒店"
    }
  }
}

7、 按评分检索过滤

GET bus/_search
{
  "version": true,
  "min_score":"2.3", #大于2.3
  "from": 0,
  "size": 3,
  "query": {
    "match": {
      "desc": "机场酒店"
    }
  }
}

8、 高亮关键字

GET bus/_search
{
  "version": true,
  "from": 0,
  "size": 3,
  "query": {
    "match": {
      "desc": "机场酒店"
    }
  }
  , "highlight": {
    "fields": {
      "desc": {}
    }
  }
}

9、  短语匹配match_phrase

与match query类似,但用于匹配精确短语,分词后所有词项都要出现在该字段中,字段中的词项顺序要一致。

GET bus/_search
{
  "query": {
    "match_phrase": {
      "name": "公交车122"
    }
  }
}

{  "took" : 4,  "timed_out" : false,  "_shards" : {    "total" : 3,    "successful" : 3,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : 1,    "max_score" : 3.4102418,    "hits" : [      {        "_index" : "bus",        "_type" : "product",        "_id" : "3",        "_score" : 3.4102418,        "_source" : {          "name" : "公交车122路",          "desc" : "从前兴路枢纽到东站",          "price" : 2,          "producer" : "公交集团",          "tags" : [            "单层",            "空调"          ]        }      }    ]  }}

对比matchGET bus/_search{  "query": {    "match": {      "name": "公交车122"    }  }}

{  "took" : 1,  "timed_out" : false,  "_shards" : {    "total" : 3,    "successful" : 3,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : 3,    "max_score" : 5.3417225,    "hits" : [      {        "_index" : "bus",        "_type" : "product",        "_id" : "2",        "_score" : 5.3417225,        "_source" : {          "name" : "公交车5路",          "desc" : "从巫家坝到梁家河",          "price" : 1,          "producer" : "公交集团",          "tags" : [            "双层",            "普通",            "热门"          ]        }      },      {        "_index" : "bus",        "_type" : "product",        "_id" : "3",        "_score" : 3.4102418,        "_source" : {          "name" : "公交车122路",          "desc" : "从前兴路枢纽到东站",          "price" : 2,          "producer" : "公交集团",          "tags" : [            "单层",            "空调"          ]        }      },      {        "_index" : "bus",        "_type" : "product",        "_id" : "1",        "_score" : 2.1597636,        "_source" : {          "name" : "公交车5路",          "desc" : "从巫家坝到梁家河",          "price" : 1,          "producer" : "公交集团",          "tags" : [            "双层",            "普通",            "热门"          ]        }      }    ]  }} 

10、短语前缀查询match_phrase_prefix

match_phrase_prefix与match_phrase相同,只是它允许在文本中的最后一个词的前缀匹配

GET bus/_search
{
  "query": {
    "match_phrase_prefix": {
      "name": "公交车1"
    }
  }
}

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 6.8204837,
    "hits" : [
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "3",
        "_score" : 6.8204837,
        "_source" : {
          "name" : "公交车122路",
          "desc" : "从前兴路枢纽到东站",
          "price" : 2,
          "producer" : "公交集团",
          "tags" : [
            "单层",
            "空调"
          ]
        }
      }
    ]
  }
}

对比:
GET bus/_search
{
  "query": {
    "match_phrase": {
      "name": "公交车1"
    }
  }
}

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

11、 多字段查询multi_match

GET bus/_search
{
  "query": {
    "multi_match": {
      "query": "空港",
      "fields": ["desc","name"]
    }
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 3.6836727,
    "hits" : [
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "16",
        "_score" : 3.6836727,
        "_source" : {
          "name" : "机场大巴A2线",
          "desc" : "空港",
          "price" : 21,
          "producer" : "大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ]
        }
      },
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "18",
        "_score" : 3.5525968,
        "_source" : {
          "name" : "空港大巴A2线",
          "desc" : "机场",
          "price" : 21,
          "producer" : "大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ]
        }
      },
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "19",
        "_score" : 3.1757839,
        "_source" : {
          "name" : "空港大巴A2线",
          "desc" : "空港快线",
          "price" : 21,
          "producer" : "大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ]
        }
      }
    ]
  }
}

12、范围检索,range查询用于匹配数值型、日期型或字符串型字段在某一范围内的文档。

GET bus/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "range": {
      "updateDate": {
        "gte": "2018-12-01",
        "lte": "2018-12-31",        "format": "yyyy-MM-dd"
      }
    }
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "13",
        "_score" : 1.0,
        "_source" : {
          "name" : "机场大巴A2线",
          "desc" : "机机场场",
          "price" : 21,
          "producer" : "机场大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ],
          "updateDate" : "2018-12-20"
        }
      },
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "12",
        "_score" : 1.0,
        "_source" : {
          "name" : "机场大巴A2线",
          "desc" : "机机场场",
          "price" : 21,
          "producer" : "机场大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ],
          "updateDate" : "2018-12-01"
        }
      }
    ]
  }
}

可以不加索引,表示搜索所有索引。

GET /_search
{
  "from": 0,
  "size": 10,
  "query": {
    "range": {
      "updateDate": {
        "gte": "2018-12-01",
        "lte": "2018-12-31",        "format": "yyyy-MM-dd"
      }
    }
  }
}

{
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 22,
    "successful" : 20,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "13",
        "_score" : 1.0,
        "_source" : {
          "name" : "机场大巴A2线",
          "desc" : "机机场场",
          "price" : 21,
          "producer" : "机场大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ],
          "updateDate" : "2018-12-20"
        }
      },
      {
        "_index" : "home",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "My first post3",
          "memo" : "a test23",
          "updateDate" : "2018-12-13"
        }
      },
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "12",
        "_score" : 1.0,
        "_source" : {
          "name" : "机场大巴A2线",
          "desc" : "机机场场",
          "price" : 21,
          "producer" : "机场大巴",
          "tags" : [
            "单层",
            "空调",
            "大巴"
          ],
          "updateDate" : "2018-12-01"
        }
      },
      {
        "_index" : "home",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "My first post2",
          "memo" : "a test2",
          "updateDate" : "2018-12-12"
        }
      }
    ]
  }
}

13、检索字段或者值是否存在,exists查询

只能返回field字段存在,并且不为null的数据。

GET /_search
{
  "query": {
    "exists": {"field": "desc1"}
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 22,
    "successful" : 20,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

14、prefix query前缀查询,Matches documents that have fields containing terms with a specified prefix (not analyzed)

GET _search
{
  "from": 0,
  "size": 2,
  "query": {
    "prefix": {
      "name": "公" #根据分词
    }
  }
}

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 22,
    "successful" : 20,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "公交车5路",
          "desc" : "从巫家坝到梁家河",
          "price" : 1,
          "producer" : "公交集团",
          "tags" : [
            "双层",
            "普通",
            "热门"
          ],
          "updateDate" : "2018-03-01"
        }
      },
      {
        "_index" : "bus",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "公交车5路",
          "desc" : "从巫家坝到梁家河",
          "price" : 1,
          "producer" : "公交集团",
          "tags" : [
            "双层",
            "普通",
            "热门"
          ],
          "updateDate" : "2018-02-01"
        }
      }
    ]
  }
}

15、wildcard查询(通配符查询)

支持*和?通配查询.

GET /_search
{
    "query": {
        "wildcard" : { "user" : "ki*y" }
    }
}

16、regexp查询(正则表达式查询)

https://www.elastic.co/guide/en/elasticsearch/reference//6.5/query-dsl-regexp-query.html

17、模糊查询fuzzy query

https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-fuzzy-query.html

18、ids查询

GET /_search
{
  "query": {
    "ids": {
      "type": "product",
      "values": [1,2]
    }
  }
}

原文地址:https://www.cnblogs.com/asker009/p/10099890.html

时间: 2024-08-30 13:26:28

Elasticsearch 检索相关的相关文章

Elasticsearch 检索

说到查询,那么索引也是一个绕不开的话题,可以说,没有索引就没有检索,先来看一个示意图 左边是索引过程,右边是检索过程.关键的步骤是分词过程,我用等号表示这两个过程一样,而且,必须一样,这个等号并不是模糊的流程的相同,而且必须是逻辑也相同. 简单来讲,采用的分词器和分词流程需要一样,否则,很有可能填进去的文档,搜不出来. 本篇重点在如何用JAVA API实现查询上,就不深入说索引的过程了,以后有时间再深入. 对于查询,有两个基础的类型,Match查询,和Term查询.这也是跟索引过程相关的.刚开始

索引的检索相关代码

常用类简介 Directory 指定索引所在目录 FSDirectory 存放于磁盘上的文件系统 RAMDirectory 存放于内存中的目录,用于测试等用途 如Directory directory=FSDirectory.open(new File("filePath")); IndexReader 读索引,依靠Directory类初始化 如 IndexReader reader=DirectoryReader.open(directory); IndexSearcher 用于索引

Windows下ElasticSearch及相关插件的安装

(1)在官网下载ElasticSearch压缩包.这里我下载的是elasticsearch-1.7.1(下载地址:https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.1.zip). (2)选择电脑上的某个位置进行解压,这里我是放在了D:\elasticsearch-1.7.1. (3)进入bin文件夹.找到elasticsearch.bat文件,双击运行. 在bin的同级文件夹下会生成data和log

分布式搜索引擎Elasticsearch的查询与过滤

一.写入 先来一个简单的官方例子,插入的参数为 -XPUT ,插入一条记录. curl -XPUT'http://localhost:9200/test/users/1' -d'{ "user": "test", "post_date": "2009-11-15T14:12:12", "message": "Elastic Search" }' { "_index":

lucene和ElasticSearch基本概念

lucene基本概念 索引(Index) 对应一个倒排表,一个检索的基本单位.在lucene中就对应一个目录. lucene基本概念 段(Segment) 一个索引可以包含多个段,段与段之间是独立的,添加新文档可以生成新的段,不同的段可以合并.段是索引数据存储的单元. 文档(Document) ?文档是我们建索引的基本单位,不同的文档是保存在不同的段中的,一个段可以包含多篇文档. ?新添加的文档是单独保存在一个新生成的段中,随着段的合并,不同的文档合并到同一个段中. 域(Field) ?一篇文档

Elasticsearch之基本操作

elasticsearch是一个是开源的(Apache2协议),分布式的,RESTful的,构建在Apache Lucene之上的的搜索引擎. 它有很多特点例如Schema Free,Document Oriented.它是#nosql的,基于JSON,同时支持多种API,包括HTTP, thrift, memcached.支持HTTP,是比较爽的一点,因为基本上所有的应用都可以用ES了,页面上的js脚本都可以去查询. 安装 启动和安装特别简单,在ES下载页面下载zip或者tar包后,解压,然后

Elasticsearch学习,请先看这一篇!

题记: Elasticsearch研究有一段时间了,现特将Elasticsearch相关核心知识.原理从初学者认知.学习的角度,从以下9个方面进行详细梳理.欢迎讨论-- 0. 带着问题上路--ES是如何产生的? (1)思考:大规模数据如何检索? 如:当系统数据量上了10亿.100亿条的时候,我们在做系统架构的时候通常会从以下角度去考虑问题: 1)用什么数据库好?(mysql.sybase.oracle.达梦.神通.mongodb.hbase-) 2)如何解决单点故障:(lvs.F5.A10.Zo

如何开发自己的搜索帝国之Elasticsearch

搜索引擎是什么? 搜索引擎是指根据一定的策略.运用特定的计算机程序从互联网上搜集信息,在对信息进行组织和处理后,为用户提供检索服务,将用户检索相关的信息展示给用户的系统.搜索引擎包括全文索引.目录索引.元搜索引擎.垂直搜索引擎.集合式搜索引擎.门户搜索引擎与免费链接列表等. Elasticsearch是什么? Elasticsearch一个高可扩展的开源的全文本搜索和分析工具.它允许你以近实时的方式快速存储.搜索.分析大容量的数据.Elasticsearch不仅提供全文检索功能,还能提供高效的分

搜索引擎solr和elasticsearch

刚開始接触搜索引擎,网上收集了一些资料.在这里整理了一下分享给大家. 一.关于搜索引擎 搜索引擎(Search Engine)是指依据一定的策略.运用特定的计算机程序从互联网上搜集信息.在对信息进行组织和处理后,为用户提供检索服务.将用户检索相关的信息展示给用户的系统. 搜索引擎包含全文索引.文件夹索引.元搜索引擎.垂直搜索引擎.集合式搜索引擎.门户搜索引擎与免费链接列表等. 一个搜索引擎由搜索器 .索引器 .检索器 和用户接口 四个部分组成.搜索器的功能是在互联网 中漫游,发现和搜集信息.索引