Search API 的分类
ElasticSearch 的查询api分为两大类 URL Search 和 Request Body Search
- URL Search
- http get 的方式
- url中使用查询参数
- Request Body Search
- ElasticSearch 提供的,基于 json格式的更完备的查询语言,get post
指定查询索引
/_search | 集群上的所有索引 |
/index1/_search | index1索引中查询 |
/index1,index2/_search | 在index1和index2 索引中查询 |
/index*/_search | 可使用通配符 index* 中查询 |
URl 查询的样例
在
所有索引
中的customer_first_name
这个字段中搜索值为 Eddit
的文档
指定字段查询可使用上面的 q=查询字段:查询值
或则使用df字段
指定字段查询
# 查询示例 #基本查询 GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s #带profile , 如果不指定字段,es会对文档中的所有字段进行查询,性能降低 GET /movies/_search?q=2012&df=title { "profile":"true" } #泛查询,正对_all,所有字段 GET /movies/_search?q=2012 { "profile":"true" } #指定字段 GET /movies/_search?q=title:2012&sort=year:desc&from=0&size=10&timeout=1s { "profile":"true" }
Term Query & Phrase Query
#使用引号,Phrase查询 GET /movies/_search?q=title:"Beautiful Mind" { "profile":"true" } # 查找美丽心灵, Mind为泛查询 GET /movies/_search?q=title:Beautiful Mind { "profile":"true" } #分组,Bool查询 GET /movies/_search?q=title:(Beautiful Mind) { "profile":"true" }
#布尔操作符 # 查找美丽心灵 GET /movies/_search?q=title:(Beautiful AND Mind) { "profile":"true" } # 查找美丽心灵 GET /movies/_search?q=title:(Beautiful NOT Mind) { "profile":"true" } # 查找美丽心灵 GET /movies/_search?q=title:(Beautiful %2BMind) { "profile":"true" }
#范围查询 ,区间写法 GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018%7D { "profile":"true" }
#通配符查询 GET /movies/_search?q=title:b* { "profile":"true" } //模糊匹配&近似度匹配 GET /movies/_search?q=title:beautifl~1 { "profile":"true" } GET /movies/_search?q=title:"Lord Rings"~2 { "profile":"true" }
Request Body 样例
返回信息:
https://www.hugbg.com/archives/1978.html
2020-04-18 6:53 下午 1F
我们总是喜欢拿“顺其自然”来敷衍人生道路上的荆棘坎坷,却很少承认,真正的顺其自然,其实是竭尽所能之后的不强求,而非两手一摊的不作为。 —网易云 花びらの刻
2020-04-18 7:08 下午 2F
人生的真理,只是藏在平淡无味之中。 —佛教禅语
2020-04-18 7:45 下午 3F
弱小和无知,都不是生存的障碍,傲慢才是。 —三体