elasticsearch-查询

使用如下语句创建一个名字为:user_document,别名为user的索引

PUT:http://localhost:9200/user_document

{
     "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0
    },
    "mappings" : {
        "user_1" : {
            "properties" : {
                "Identity" :
                {
                    "type" : "string",
                    "index" : "not_analyzed"
                },
                 "Name" :
                {
                    "type" : "string"
                },
                 "Age" :
                {
                    "type" : "integer"
                },
                 "Birthday" :
                {
                    "format": "dateOptionalTime",
                    "type": "date"
                },
                 "Sex" :
                {
                    "type" : "boolean"
                }
            }
        }
    },
    "aliases" : {
        "user" : {}
    }
}

并新增10条测试数据

1、Match All 查询

GET _search
{
    "query":
    {
        "match_all": {}
    }
}

匹配索引中的全部文档,默认得分为1.0

2、Match 查询

GET _search
{
    "query":
    {
        "match":
        {
            "Name":"陈楚生"
        }
    }
}

结果

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1.8677558,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 1.8677558,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcPTKOpxK9K1OdHXH",
            "_score": 0.6856395,
            "_source": {
               "Identity": "360501199001016212",
               "Name": "陈楚河",
               "Age": 40,
               "Birthday": "1978-05-09T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcnK7OpxK9K1OdHXL",
            "_score": 0.14631948,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "陈乔恩",
               "Age": 36,
               "Birthday": "1980-05-09T14:12:12",
               "Sex": false
            }
         }
      ]
   }
}

Match查询字符串时,会将文本内容分词并做Or操作

3、Multi Match 查询

GET _search
{
    "query":
    {
        "multi_match": {
           "query": "陈楚生",
           "fields": ["Name","Identity"]
        }
    }
}

同Match查询,但是可以同时查多个字段

4、Query String 查询

GET _search
{
    "query":
    {
        "query_string": {
           "default_field": "Name",
           "query": "陈楚生"
        }
    }
}

结果

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1.8677558,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 1.8677558,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcPTKOpxK9K1OdHXH",
            "_score": 0.6856395,
            "_source": {
               "Identity": "360501199001016212",
               "Name": "陈楚河",
               "Age": 40,
               "Birthday": "1978-05-09T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDcnK7OpxK9K1OdHXL",
            "_score": 0.14631948,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "陈乔恩",
               "Age": 36,
               "Birthday": "1980-05-09T14:12:12",
               "Sex": false
            }
         }
      ]
   }
}

分词查询,也可以想同时查询多个字段

5、Term 查询

GET _search
{
    "query":
    {
        "term": {
           "Identity": {
              "value": "360501199001016210",
              "boost": 2
           }
        }
    }
}

结果

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 2.5040774,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 2.5040774,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         }
      ]
   }
}

Term查询一般用来做精确查询,即查询关键字需要和被查询字段值完全一致才会被查询出来。

所以如果当被查询的字段是一个String类型时,该类型需要被显示标记为not_analyzed,

否则字段内容会先被分词器分成许多个词条,导致term查询不出结果。

可以使用Match 或Query_String查询会被分词的字段。

6、Terms 查询

GET _search
{
    "query":
    {
        "terms": {
           "Age": [
              35,30
           ]
        }
    }
}

结果

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.8853251,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 0.8853251,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDc4xqOpxK9K1OdHXN",
            "_score": 0.8853251,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "唐嫣",
               "Age": 30,
               "Birthday": "1986-05-09T14:12:12",
               "Sex": true
            }
         }
      ]
   }
}

查询一个值数组范围内的所有文档

7、Range 查询

GET _search
{
    "query":
    {
        "range": {
           "Age": {
              "from": 25,
              "to": 35
           }
        }
    }
}

结果

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDbtj0OpxK9K1OdHXF",
            "_score": 1,
            "_source": {
               "Identity": "360501199001016210",
               "Name": "陈楚生",
               "Age": 35,
               "Birthday": "1981-07-25T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDch_LOpxK9K1OdHXK",
            "_score": 1,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "赵默笙",
               "Age": 28,
               "Birthday": "1988-05-09T14:12:12",
               "Sex": false
            }
         },
         {
            "_index": "user_document",
            "_type": "user_1",
            "_id": "AVYDc4xqOpxK9K1OdHXN",
            "_score": 1,
            "_source": {
               "Identity": "360501199001016214",
               "Name": "唐嫣",
               "Age": 30,
               "Birthday": "1986-05-09T14:12:12",
               "Sex": true
            }
         }
      ]
   }
}

范围查询

时间: 2024-11-05 19:28:24

elasticsearch-查询的相关文章

Func<T,T>应用之Elasticsearch查询语句构造器的开发

前言 之前项目中做Elasticsearch相关开发的时候,虽然借助了第三方的组件PlainElastic.Net,但是由于当时不熟悉用法,而选择了自己拼接查询语句.例如: string queryGroup = "{\"query\": {\"match\": { \"roomid\": \"FRIEND_12686_10035\" }}}"; //关键字查询 string queryKeyWord =

Elasticsearch入门教程(五):Elasticsearch查询(一)

原文:Elasticsearch入门教程(五):Elasticsearch查询(一) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/79228852 // 准备数据 PUT /shop/goods/1 { "name": "2017新款女装冬季外套ulzzang棉袄中长款棉衣韩版百搭棉服面包服", &quo

Elasticsearch入门教程(六):Elasticsearch查询(二)

原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/79237950 地理坐标点geo-point 地理坐标点:是指地球表面可以用经纬度描述的一个点. 地理坐标点可以用来计算两个坐标间的距离,还可以判断一个坐标是否在一个区域中,或在聚合中. 地理坐标点不能被

elasticsearch查询模板

最近在公司又用到了elasticsearch,也用到了查询模板,顺便写篇文章记录一下查询模板的使用. 以1个需求为例讲解es模板的使用: 页面上某个按钮在一段时间内的点击次数统计,并且可以以小时,天,月为单位进行汇总,并且需要去重. 创建索引,只定义3个字段,user_id, user_name和create_time: -POST /$ES/event_index { "mappings": { "event": { "_ttl": { &qu

ElasticSearch查询 第四篇:匹配查询(Match)

匹配(Match)查询属于全文(Fulltext)查询,不同于词条查询,ElasticSearch引擎在处理全文搜索时,首先分析(analyze)查询字符串,然后根据分词构建查询,最终返回查询结果.匹配查询共有三种类型,分别是布尔(boolean).短语(phrase)和短语前缀(phrase_prefix),默认的匹配查询是布尔类型,这意味着,ElasticSearch引擎首先分析查询字符串,根据分析器对其进行分词,例如,对于以下match查询: "query":{ "ma

Elasticsearch 查询与过滤

今天在给上级汇报工作的时候,被问到了这个问题,一时也没回到上来. 英文原文: https://www.elastic.co/guide/en/elasticsearch/guide/current/_queries_and_filters.html 前面我们讲到的是关于结构化查询语句,事实上我们可以使用两种结构化语句: 结构化查询(Query DSL)和结构化过滤(Filter DSL). 查询与过滤语句非常相似,但是它们由于使用目的不同而稍有差异. 一条过滤语句会询问每个文档的字段值是否包含着

Elasticsearch查询类型

Elasticsearch支持两种类型的查询:基本查询和复合查询. 基本查询,如词条查询用于查询实际数据. 复合查询,如布尔查询,可以合并多个查询, 然而,这不是全部.除了这两种类型的查询,你还可以用过滤查询,根据一定的条件缩小查询结果.不像其他查询,筛选查询不会影响得分,而且通常非常高效. 更加复杂的情况,查询可以包含其他查询.此外,一些查询可以包含过滤器,而其他查询可同时包含查询和过滤器.这并不是全部,但暂时先解释这些工作. 1.简单查询 这种查询方式很简单,但比较局限. 查询last_na

(转)Elasticsearch查询规则------match和term

es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询全部员工 GET /megacorp/employee/_search?q=last_name:Smith //查询last_name为Smith的员工 另外一种是通过DSL语句来进行查询,被称为DSL查询(Query DSL),DSL是Elasticsearch提供的一种丰富且灵活的查询语言,该语

Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询

Elasticsearch  的查询很灵活,并且有Filter,有分组功能,还有ScriptFilter等等,所以很强大.下面上代码: 一个简单的查询,返回一个List<对象> .. /**     * 根据Id 查询 SOBangg     * @param key     * @return     */    public static List findSOBanggById(String id) {        Client client = ESTools.client;    

ElasticSearch查询字符串

简易搜索 search API有两种表单:一种是"简易版"的查询字符串(query string)将所有参数通过查询字符串定义,另一种版本使用JSON完整的表示请求体(request body),这种富搜索语言叫做结构化查询语句(DSL) 查询字符串搜索对于在命令行下运行点对点(ad hoc)查询特别有用.例如这个语句查询所有类型为tweet并在tweet字段中包含elasticsearch字符的文档: GET /_all/tweet/_search?q=tweet:elasticse