1. 学习查询的api语法
首先向es中titles写入两个文档
POST titles/_doc/1
{
"title": "Quick brown rabbits",
"body": "Brown rabbits are commonly seen."
}
POST titles/_doc/2
{
"title": "Keeping pets healthy",
"body": "My quick brown fox eats rabbits on a regular basis."
}
2 . 查询 tille 和body中 全文检索 quick brown的内容,语法如下
1. 首先在写入文档的时候会先对写入的内容进行分词,分词后写入倒排索引,查询的时候也会对查询内容进行分词,分词后和倒排索引进行匹配,进行相关度查询
POST titles/_search { "query": { "bool": { "should": [ {"match": {"title": "quick brown"}}, {"match": {"body": "quick brown"}} ] } } }
结果如下
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 2.1213155, "hits" : [ { "_index" : "titles", "_type" : "_doc", "_id" : "1", "_score" : 2.1213155, "_source" : { "title" : "Quick brown rabbits", "body" : "Brown rabbits are commonly seen." } }, { "_index" : "titles", "_type" : "_doc", "_id" : "2", "_score" : 0.55788946, "_source" : { "title" : "Keeping pets healthy", "body" : "My quick brown fox eats rabbits on a regular basis." } } ] } }
可以看到查询结果, 匹配到两个文档,文档1的评分比文档2的评分要高,直观感受应该是文档2的评分更匹配,这是因为should 查询评分是叠加的关系,会对body 和titile 中查询字段评分进行叠加 1,中body和title 中都有brown
2 中的titile没有匹配,虽然body内容更加匹配。 有时候我们需要查询某个字段更加匹配展示给用户,可以使用dis_max 查询
POST titles/_search { "query": { "multi_match": { "query": "brown fox", "fields": ["title","body"], "type": "best_fields", "tie_breaker": 0.2 } } }
说明下 ,tile_breaker 是对除了最匹配的字段外,提升其他字段的权重得分.默认是0 也就是除了最匹配外,其他字段都忽略。
查询结果如下,可以看到文档2的评分比文档1高,查询也可以修改字段的权重,title^10 标识把title查询的权重提升10倍。 默认title和body的权重是一样的。 这个可以根据实际业务来看
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.77041256, "hits" : [ { "_index" : "titles", "_type" : "_doc", "_id" : "2", "_score" : 0.77041256, "_source" : { "title" : "Keeping pets healthy", "body" : "My quick brown fox eats rabbits on a regular basis." } }, { "_index" : "titles", "_type" : "_doc", "_id" : "1", "_score" : 0.735369, "_source" : { "title" : "Quick brown rabbits", "body" : "Brown rabbits are commonly seen." } } ] } }
参考: Elasticsearch核心技术与实战