对应关系
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
搜索类型
简单搜索
使用Get关键词,GET /megacorp/employee/_search?q=last_name:Smith
q=用来传递参数
DSL搜索
DSL(Domain Specific Language特定领域语言)以JSON类型体现:
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
如果想再添加限定条件可以使用如下语句:
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"filter": [
{
"range": {
"age" : { "gt" : 30 }
}
},
{
"match" : {
"last_name" : "smith"
}
}
]
}
}
}
匹配了last_name为smith以及年龄大于30的文档。
全文搜索
我们将会搜索所有喜欢“rock climbing”的员工:
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
结果为:
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.4167401,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.4167401, <1>
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.4589591, <2>
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
<1>,<2>为相关性,由于1能匹配到rock climbing,而2只有rock能匹配,所以1的相关性会大于2,相关性这个概念在传统的关系型数据库是不存在的。
短语搜索
我就想匹配rock climbing整个可以使用下面语句:
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
高亮
可以用em标签标记我想搜索的关键词:
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}