参数解释
示例:
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
"profile":"true"
}
- q:置顶查询关键字 使用query string syntax
- df:置查询字段,其中总置顶查询title中包含2012的结果
- sort:用于排序 from和size用于分页
- profile:可以查看查询是怎么运行的
接下来会详细介绍query string syntax
- 指定字段和范查询
q=title:2012置顶字段;
q=2012泛查询
GET /movies/_search?q=2012
{
"profile":"true"
}

我们可以看到查询结果中查询了所有字段
指定字段查询
GET /movies/_search?q=title:2012
{
"profile":"true"
}

查询结果中只查询了title字段
- term vs phrase
其中的term和上面的
GET /movies/_search?q=title:2012
{
"profile":"true"
}
这个就是使用的termQuery
phrase query:查询电影title中包含Beautiful AND Mind
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile":"true"
}

- boolQuery AND OR NOT这三种语法都能用 不过必须大写 也可以使用 && || !
查询title中包含Beautiful 或者 Mind 的电影
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile":"true"
}

- 分组
-- +标识must也就是必须包含
-- -标识must not标识必须不包含
GET /movies/_search?q=title:(Beautiful +Mind)
{
"profile":"true"
}

- 通配符查询 不过这个查询可想而知的是查询效率很低:
-- ?代表1个字符
-- *代表0个或者多个字符
GET /movies/_search?q=title:b*
{
"profile":"true"
}
查询title中包含b开头
- 模糊匹配和近似查询
-- 使用~1标识模糊匹配
-- 使用“lord rings”~2标识匹配两个单词中间还能出现两个词
GET /movies/_search?q=title:beautifl~1
{
"profile":"true"
}
beautifl~1 可匹配beautifly

GET /movies/_search?q=title:"Lord Rings"~2
{
"profile":"true"
}
"Lord Rings"~2 匹配上了"Lord of the Rings

小结
本章介绍了URI Search 其中主要介绍了query string syntax的用法
网友评论