在上一篇传送门中我们介绍了URI search查询的Query String Syntax大部分的用法,而其他的URI search参数由于篇幅问题,我们当时也没有进行详细的叙述,这里我们将补充说明一些参数的用法。
这里的目录也是衔接上一篇的文章的。
二、df的用法
GET bank/_search?q=Dena&df=firstname
df 默认字段,不指定时,会对所有字段进行查询,
GET bank/_search?q=firstname:Dena两者是相同的意思,查询的方式也是一样的,只是写法不同而已
返回结果:
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 6.5042877,"hits" : [{"_index" : "bank","_type" : "account","_id" : "102","_score" : 6.5042877,"_source" : {"account_number" : 102,"balance" : 29712,"firstname" : "Dena","lastname" : "Olson","age" : 27,"gender" : "F","address" : "759 Newkirk Avenue","employer" : "Hinway","email" : "denaolson@hinway.com","city" : "Choctaw","state" : "NJ"}}]}
}
三、analyzer的使用
GET bank/_search?q=Dena&df=firstname&_source=true&analyzer=ik_smart
analyzer实际上指的是按照指定的分词器来分词。
{"took" : 27,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 6.5042877,"hits" : [{"_index" : "bank","_type" : "account","_id" : "102","_score" : 6.5042877,"_source" : {"account_number" : 102,"balance" : 29712,"firstname" : "Dena","lastname" : "Olson","age" : 27,"gender" : "F","address" : "759 Newkirk Avenue","employer" : "Hinway","email" : "denaolson@hinway.com","city" : "Choctaw","state" : "NJ"}}]}
}
四、lowercase_expanded_terms
这个好像在新版本中被移除啦,不仅在文档里没有查到,而且我在使用时也会报错。
所以不需要了
GET bank/_search
{"query": {"query_string": {"fields": ["firstname"], "query": "Dena","lowercase_expanded_terms": false}}
}
{"error" : {"root_cause" : [{"type" : "parsing_exception","reason" : "[query_string] query does not support [lowercase_expanded_terms]","line" : 6,"col" : 35}],"type" : "parsing_exception","reason" : "[query_string] query does not support [lowercase_expanded_terms]","line" : 6,"col" : 35},"status" : 400
}
五、from、size、timeout、sort
GET bank/_search?q=Dena*&df=firstname&size=5&from=0&timeout=1s&sort=age:desc
这里使用sort的field一定要是能索引的,不然无法使用sort,因为没有索引无法执行聚合和排序
这里的from是跳过第0条的意思。
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "bank","_type" : "account","_id" : "102","_score" : null,"_source" : {"account_number" : 102,"balance" : 29712,"firstname" : "Dena","lastname" : "Olson","age" : 27,"gender" : "F","address" : "759 Newkirk Avenue","employer" : "Hinway","email" : "denaolson@hinway.com","city" : "Choctaw","state" : "NJ"},"sort" : [27]}]}
}
通常的用法大概就这么多了,最近由于负责的工作更多了,所以后面估计写博客的时间少之又少了,不过还是会坚持一周至少写一篇的。这篇博客本来还要把剩余的一些uri search的用法全部写出来的,但是实在没时间了。还是自己查文档吧!!!传送门