Elasticsearch核心技术与实战学习笔记 第三章14 URI Search 详解

一 序

   本文属于极客时间Elasticsearch核心技术与实战学习笔记系列。

本节课详细介绍URI Search 。

二 URI Search

通过 URI query 实现搜索

#基本查询
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
  • q 指定查询语句,使用 Query String Syntax
  • df 默认字段,不指定时,会对所有字段进行查询
  • Sort 排序 /from 和 size 用于分页
  • Profile 可以查看查询时如何被执行的

2.1Query String Synctax

  • 指定字段 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"

这根相信的不一样,所以需要引入分组的概念。

分组和引号

  • title:(Beautiful AND Mind)
  • title=”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",

范围查询

  • 区间表示:[] 闭区间 ,{} 开区间
    • year:{2019 TO 2018}
    • year:[* TO 2018]
#范围查询 ,区间写法
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018%7D
{
	"profile":"true"
}

profile:        "description" : "+title:beautiful +year:[2002 TO 2017]", 

算数符号

  • year:>2010
  • year(>2010 && <=2018)
  • year:(+>2010 +<=2018)
//范围查询,区间写法  / 数学写法
GET  /movies/_search?q=year:>=1980
{
"profile": "true"
}

profile:                "type" : "IndexOrDocValuesQuery",
                "description" : "year:[1980 TO 9223372036854775807]",

通配符查询

(通配符查询效率低,占用内容大,不建议使用。特别是放在最前面)

  • ?代表 1 个字符,* 代表 0 或多个字符
    • title:mi?d
    • title:be*
#通配符查询
GET /movies/_search?q=title:b*
{
	"profile":"true"
}

profile:                "type" : "MultiTermQueryConstantScoreWrapper",
                "description" : "title:b*",

  • 正则表达
    • title:[bt]oy
  • 模糊匹配与近似查询
    • title:befutifl~1
    • title:”lord rings” ~2
//模糊匹配&近似度匹配:用户输错,还能找到
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,去看底层是怎么查找的。

猜你喜欢

转载自blog.csdn.net/bohu83/article/details/106148242