更多干货
分布式实战(干货)
spring cloud 实战(干货)
mybatis 实战(干货)
spring boot 实战(干货)
React 入门实战(干货)
构建中小型互联网企业架构(干货)
python 学习持续更新
1_ElatisSearch 使用 term filter 来搜索数据
2_ElatisSearch filter 执行原理 bitset 机制与 caching 机制
3_ElatisSearch 基于 bool 组合多个 filter 条件来搜索数据
4_ElatisSearch 使用 terms 搜索多个值
5_ElatisSearch 基于 range filter 来进行范围过滤
7_ElatisSearch term+bool 实现的 multiword 搜索原理
日志管理 ELK
概述
1 全文检索的时候, 进行多个值的检索, 有两种做法, match query;should
2 控制搜索结果精准度: and operator,minimum_should_match
例子
1 为帖子数据增加标题 (title) 字段
- POST /forum/article/_bulk
- { "update": { "_id": "1"} }
- { "doc" : {"title" : "this is java and elasticsearch blog"} }
- { "update": { "_id": "2"} }
- { "doc" : {"title" : "this is java blog"} }
- { "update": { "_id": "3"} }
- { "doc" : {"title" : "this is elasticsearch blog"} }
- { "update": { "_id": "4"} }
- { "doc" : {"title" : "this is java, elasticsearch, hadoop blog"} }
- { "update": { "_id": "5"} }
- { "doc" : {"title" : "this is spark blog"} }
2 搜索标题中包含 java 或 elasticsearch 的 blog
这个, 就跟之前的那个 term query, 不一样了不是搜索 exact value, 是进行 full text 全文检索
match query, 是负责进行全文检索的当然, 如果要检索的 field, 是 not_analyzed 类型的, 那么 match query 也相当于 term query
GET /forum/article/_search { "query": { "match": { "title": "java elasticsearch" } } }
3 搜索标题中包含 java 和 elasticsearch 的 blog
搜索中的文章必须 同时包含 java 和 elasticsearch
使用 and 关键字, 如果你是希望所有的搜索关键字都要匹配的, 那么就用 and, 可以实现单纯 match query 无法实现的效果
- GET /forum/article/_search
- {
- "query": {
- "match": {
- "title": {
- "query": "java elasticsearch",
- "operator": "and"
- }
- }
- }
- }
4 搜索包含 java,elasticsearch,spark,hadoop,4 个关键字中, 至少 3 个的 blog
指定一些关键字中, 必须至少匹配其中的多少个关键字, 才能作为结果返回
- GET /forum/article/_search
- {
- "query": {
- "match": {
- "title": {
- "query": "java elasticsearch spark hadoop",
- "minimum_should_match": "75%"
- }
- }
- }
- }
5 用 bool 组合多个搜索条件, 来搜索 title
- GET /forum/article/_search
- {
- "query": {
- "bool": {
- "must": { "match": { "title": "java" }},
- "must_not": { "match": { "title": "spark" }},
- "should": [
- { "match": { "title": "hadoop" }},
- { "match": { "title": "elasticsearch" }}
- ]
- }
- }
- }
6bool 组合多个搜索条件, 如何计算 relevance score
must 和 should 搜索对应的分数, 加起来, 除以 must 和 should 的总数
排名第一: java, 同时包含 should 中所有的关键字, hadoop,elasticsearch
排名第二: java, 同时包含 should 中的 elasticsearch
排名第三: java, 不包含 should 中的任何关键字
should 是可以影响相关度分数的
must 是确保说, 谁必须有这个关键字, 同时会根据这个 must 的条件去计算出 document 对这个搜索条件的 relevance score
在满足 must 的基础之上, should 中的条件, 不匹配也可以, 但是如果匹配的更多, 那么 document 的 relevance score 就会更高
搜索的结果:
- {
- "took": 6,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "failed": 0
- },
- "hits": {
- "total": 3,
- "max_score": 1.3375794,
- "hits": [
- {
- "_index": "forum",
- "_type": "article",
- "_id": "4",
- "_score": 1.3375794,
- "_source": {
- "articleID": "QQPX-R-3956-#aD8",
- "userID": 2,
- "hidden": true,
- "postDate": "2017-01-02",
- "tag": [
- "java",
- "elasticsearch"
- ],
- "tag_cnt": 2,
- "view_cnt": 80,
- "title": "this is java, elasticsearch, hadoop blog"
- }
- },
- {
- "_index": "forum",
- "_type": "article",
- "_id": "1",
- "_score": 0.53484553,
- "_source": {
- "articleID": "XHDK-A-1293-#fJ3",
- "userID": 1,
- "hidden": false,
- "postDate": "2017-01-01",
- "tag": [
- "java",
- "hadoop"
- ],
- "tag_cnt": 2,
- "view_cnt": 30,
- "title": "this is java and elasticsearch blog"
- }
- },
- {
- "_index": "forum",
- "_type": "article",
- "_id": "2",
- "_score": 0.19856805,
- "_source": {
- "articleID": "KDKE-B-9947-#kL5",
- "userID": 1,
- "hidden": false,
- "postDate": "2017-01-02",
- "tag": [
- "java"
- ],
- "tag_cnt": 1,
- "view_cnt": 50,
- "title": "this is java blog"
- }
- }
- ]
- }
- }
7 搜索 java,hadoop,spark,elasticsearch, 至少包含其中 3 个关键字
默认情况下, should 是可以不匹配任何一个的, 比如上面的搜索中, this is java blog, 就不匹配任何一个 should 条件
但是有个例外的情况, 如果没有 must 的话, 那么 should 中必须至少匹配一个才可以
比如下面的搜索, should 中有 4 个条件, 默认情况下, 只要满足其中一个条件, 就可以匹配作为结果返回
但是可以精准控制, should 的 4 个条件中, 至少匹配几个才能作为结果返回
- GET /forum/article/_search
- {
- "query": {
- "bool": {
- "should": [
- { "match": { "title": "java" }},
- { "match": { "title": "elasticsearch" }},
- { "match": { "title": "hadoop" }},
- { "match": { "title": "spark" }}
- ],
- "minimum_should_match": 3
- }
- }
- }
来源: http://blog.csdn.net/qq_27384769/article/details/79634341