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"] } } }
原文地址:https://www.cnblogs.com/javasl/p/11405387.html
时间: 2024-10-05 02:51:17