本文属于极客时间Elasticsearch核心技术与实战学习笔记系列。
本节课详细介绍URI Search 。
通过 URI query 实现搜索
#基本查询
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
指定字段 vs 泛查询
q=title:2012 / q=2012
//只对title字段进行查询
GET /movies/_search?q=2012&df=title
profile查看实现可见:
"type" : "TermQuery",
"description" : "title:2012",
#泛查询,正对_all,所有字段
GET /movies/_search?q=2012
{
"profile":"true"
}
泛查询查询你所有字段效率低,为了提高效率,也可以指定字段去查
//对自定字段进行查询 跟 df 等效
GET /movies/_search?q=title:2012
{
"profile": "true"
}
Term vs Phrase
Beautiful Mind 等效于 Beautiful OR Mind
“Beautiful Mind”, 等效于 Beautiful AND Mind。Phrase 查询,还要求前后顺序保存一致
#使用引号。Phrase
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile": "true"
}
# 查找美丽心灵, Mind为泛查询
GET /movies/_search?q=title:Beautiful Mind
{
"profile":"true"
}
// 意思就是说 title 是Term 查询 "Beautiful" ,对所有字段查询"Mind"
这根相信的不一样,所以需要引入分组的概念。
#分组,Bool查询
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile":"true"
}
title必须包含beautiful, mind.
AND / OR / NOT 或者 && / || / !
必须大写
title:(matrix NOT reloaded)
// type:BooleanQuery
// title 里面必须包括Beautiful 跟 Mind
GET /movies/_search?q=title:(Beautiful AND Mind)
{
"profile": "true"
}
profile:
"type" : "BooleanQuery",
"description" : "+title:beautiful +title:mind",
// type:BooleanQuery
//必须包括Beautiful 但不包括 Mind
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile": "true"
}
profile:
"type" : "BooleanQuery",
"description" : "title:beautiful -title:mind",
// type:BooleanQuery
//包括Beautiful必须有Mind
GET /movies/_search?q=title:(Beautiful %2BMind)
{
"profile": "true"
}
profile:
"type" : "BooleanQuery",
"description" : "title:beautiful +title:mind",
#范围查询 ,区间写法
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018%7D
{
"profile":"true"
}
profile: "description" : "+title:beautiful +year:[2002 TO 2017]",
算数符号
//范围查询,区间写法 / 数学写法
GET /movies/_search?q=year:>=1980
{
"profile": "true"
}
profile: "type" : "IndexOrDocValuesQuery",
"description" : "year:[1980 TO 9223372036854775807]",
(通配符查询效率低,占用内容大,不建议使用。特别是放在最前面)
#通配符查询
GET /movies/_search?q=title:b*
{
"profile":"true"
}
profile: "type" : "MultiTermQueryConstantScoreWrapper",
"description" : "title:b*",
//模糊匹配&近似度匹配:用户输错,还能找到
GET /movies/_search?q=title:beautifl~1
{
"profile":"true"
}
profile: "type" : "BoostQuery",
"description" : "(title:beautiful)^0.875",
GET /movies/_search?q=title:"Lord Rings"~2
{
"profile":"true"
}
profile: "type" : "PhraseQuery",
"description" : """title:"lord rings"~2""",
URL查询,属于日常维护查询操作。而不是程序里面去使用。
结合profile,去看底层是怎么查找的。