重温ES~

对应关系

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" : {}
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值