(12)ElasticSearch 基本查询(Query查询)

Posted javasl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(12)ElasticSearch 基本查询(Query查询)相关的知识,希望对你有一定的参考价值。

1、数据准备

PUT /lib3

    "settings":
        "number_of_shards":3,
        "number_of_replicas":0
      ,
        "mappings":
            "user":
                "properties":
                    "name":"type":"text",
                    "address":"type":"text",
                    "age":"type":"integer",
                    "interests":"type":"text",
                    "birthday":"type":"date"
                
            
        
put /lib3/user/1

    "name":"zhaoliu",
    "address":"hei long jiang sheng tie ling shi",
    "age":50,
    "birthday":"1970-12-12",
    "interests":"xi huang hejiu,duanlian,lvyou"


put /lib3/user/2

    "name":"zhaoming",
    "address":"bei jing hai dian qu qing he zhen",
    "age":20,
    "birthday":"1998-10-12",
    "interests":"xi huan hejiu,duanlian,changge"


put /lib3/user/3

    "name":"lisi",
    "address":"bei jing hai dian qu qing he zhen",
    "age":23,
    "birthday":"1998-10-12",
    "interests":"xi huan hejiu,duanlian,changge"


put /lib3/user/4

    "name":"wangwu",
    "address":"bei jing hai dian qu qing he zhen",
    "age":26,
    "birthday":"1998-10-12",
    "interests":"xi huan biancheng,tingyinyue,lvyou"


put /lib3/user/5

    "name":"zhangsan",
    "address":"bei jing chao yang qu",
    "age":29,
    "birthday":"1988-10-12",
    "interests":"xi huan tingyinyue,changge,tiaowu"

2、查询

  GET /lib3/user/_search?q=name:lisi  查询name是lisi的记录

  GET /lib3/user/_search?q=interests:changge&sort=age:desc  查询兴趣包含唱歌的,并且按照年龄倒序排序

  term、terms查询:

  term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date。

  term:查询某个字段里含有某个关键词的文档

  (1)查询name是zhaoliu的:

GET /lib3/user/_search/

    "query":
        "term":"name":"zhaoliu"
    

  (2)查询兴趣包含喝酒或者唱歌的:

get /lib3/user/_search

    "query":
        "terms":
            "interests":["hejiu","changge"]
        
    

  (3)查询兴趣包含喝酒或者唱歌的,下标从0开始,取出两个:

get /lib3/user/_search

    "from":0,
    "size":2,
    "query":
        "terms":
            "interests":["hejiu","changge"]
        
    

 

以上是关于(12)ElasticSearch 基本查询(Query查询)的主要内容,如果未能解决你的问题,请参考以下文章

elasticsearch基本操作之--使用QueryBuilders进行查询

elasticSearch - 高级检索

Elastic Search 基本操作

elasticsearch添加了通配符失败的查询

elasticsearch基本查询笔记(二)-- 分词查询

Elasticsearch系列(12)Query之复合查询