Elasticsearch Query&DSL搜索

ES DSL搜索

查询

term

term 是精确搜索,搜索的时候会将用户的搜索内容,比如"好的"作为一整个关键词去搜索,而不会对其进行分词后再搜索。

传递json数据

好的去搜索符合的

{
    "query": {
        "term": {
            "desc": "好的"
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}
{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.8419956,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.8419956,
                "_source": {
                    "nickname": "红帽子",
                    "id": 1004,
                    "desc": "好的系统必须拥有稳定的系统结构"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.8419956,
                "_source": {
                    "nickname": "switch游戏机",
                    "id": 1005,
                    "desc": "好的游戏,才会有人购买,比如塞尔达"
                }
            }
        ]
    }
}
terms

匹配多个关键字进行精确搜索

{
    "query": {
        "terms": {
            "desc": ["好","的"]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

好 & 的两个条件作为关键字搜索

match

match 是非精确搜索,搜索的时候会将用户的搜索内容,比如"好的"作为一整个关键词去搜索,而不会对其进行分词后再搜索。

{
    "query": {
        "match": {
            "desc": "好的"
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}
{
    "took": 85,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 3.1980762,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 3.1980762,
                "_source": {
                    "nickname": "红帽子",
                    "id": 1004,
                    "desc": "好的系统必须拥有稳定的系统结构"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 3.0979095,
                "_source": {
                    "nickname": "switch游戏机",
                    "id": 1005,
                    "desc": "好的游戏,才会有人购买,比如塞尔达"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.37556386,
                "_source": {
                    "nickname": "涡轮增压",
                    "id": 1003,
                    "desc": "极限的速度是需要涡轮增压的"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1012",
                "_score": 0.36424035,
                "_source": {
                    "nickname": "youzi",
                    "id": 1012,
                    "desc": "永远的神"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.35254776,
                "_source": {
                    "nickname": "进击的巨人",
                    "id": 1002,
                    "desc": "艾伦是会变成真正的巨人的"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 0.27665582,
                "_source": {
                    "nickname": "皮特",
                    "id": 1011,
                    "desc": "皮特的姓氏好像是彼得"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 0.2639615,
                "_source": {
                    "nickname": "老男孩",
                    "id": 1007,
                    "desc": "确实是个很好的组合,筷子 筷子"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1009",
                "_score": 0.252381,
                "_source": {
                    "nickname": "露西",
                    "id": 1009,
                    "desc": "露西是一只很聪明的cat"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.18093815,
                "_source": {
                    "nickname": "飞翔的荷兰号",
                    "id": 1001,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识"
                }
            }
        ]
    }
}
match_all

在索引中查询所有文档

{
    "query": {
        "match_all": {}
    },
    "_source": [
        "id",
        "nickname",
        "age"
    ]
}
区别

term在精确搜索到情况下,只命中了2条,而match在非精确搜索的情况下,命中了9条.

match扩展
or
{
  "query": {
    "match": {
      "desc": "好的"
    }
  }
}
# 等同于下方
{
    "query": {
        "match": {
            "desc": {
            "query": "好的",
            "operator": "and"
            }
        }
    }
}
# 相当于 select * from search_demo where desc = 'aaa' or desc='bbb'

or 当多个or连用时,满足一个条件即可返回结果。

and
{
  "query": {
    "match": {
      "desc": "好的"
    }
  }
}
# 等同于下方
{
    "query": {
        "match": {
            "desc": {
            "query": "好的",
            "operator": "and"
            }
        }
    }
}
# 相当于 select * from search_demo where desc = 'aaa'and desc='bbb'

and 当多个连用时,必须满足所有条件才返回结果。

match_phrase

match:分词后只要有匹配就返回

match_phrase:分词结果必须在text字段内容中都包含而且顺序必须相同,而且必须是连续的(搜索比较严格)

  • slop:允许词语间跳过的数量

    构建查询请求

{
    "query": {
        "match_phrase": {
            "desc": {
                "query": "皮特 姓氏",
                "slop": 1
            }
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

查询结果

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 4.7110896,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 4.7110896,
                "_source": {
                    "nickname": "皮特",
                    "id": 1011,
                    "desc": "皮特的姓氏好像是彼得"
                }
            }
        ]
    }
}
ids

ids通过id去查询,分为单个查询以及多个查询

查询单个

构建查询

GET /search_demo/_doc/1001

查询结果

{
    "_index": "search_demo",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 2,
    "found": true,
    "_source": {
        "id": 1001,
        "age": 18,
        "username": "Tic",
        "nickname": "飞翔的荷兰号",
        "money": 88.8,
        "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识",
        "sex": 0,
        "birthday": "1992-12-24",
        "face": "http://www.p2pi.cn/static/img/1001_face.png"
    }
}
查询多个

构建查询

{
    "query": {
        "ids": {
            "type": "_doc",
            "values": ["1001","1005"]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

查询结果

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
                    "nickname": "飞翔的荷兰号",
                    "id": 1001,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "nickname": "switch游戏机",
                    "id": 1005,
                    "desc": "好的游戏,才会有人购买,比如塞尔达"
                }
            }
        ]
    }
}

multi_match

multi_match 满足使用 match 在多个字段中进行查询的需求

查询条件

{
    "query": {
        "multi_match": {
            "query": "组合",
            "fields": [
                "desc","nickname"
            ]
        }
    },
        "_source": [
            "id",
            "nickname",
            "desc"
        ]
}

查询结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.2874916,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 2.2874916,
                "_source": {
                    "nickname": "老男孩",
                    "id": 1007,
                    "desc": "确实是个很好的组合,筷子 筷子"
                }
            }
        ]
    }
}

_source

返回需要查询的结果

返回nickname,id,desc 三个字段

在这里插入图片描述

boost

权重

可以将某个搜索条件的权重加大 ,数值越大越大代表着优先级越高
在这里插入图片描述

prefix

prefix 根据前缀去查询

查询条件

{
  "query": {
    "prefix": {
      "desc": "every"
    }
  }
}

查询结果

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1010",
                "_score": 1.0,
                "_source": {
                    "id": 1010,
                    "age": 30,
                    "username": "cc",
                    "nickname": "cc",
                    "money": 100.8,
                    "desc": " i cc you everyday",
                    "sex": 1,
                    "birthday": "1988-07-14",
                    "face": "http://www.p2pi.cn/static/img/1010_face.png"
                }
            }
        ]
    }
}

fuzzy

fuzzy 模糊搜索,并不是指的SQL的模糊搜索,而是用户在进行搜索的时候打字错误现象,搜索引擎会自动纠正,然后尝试匹配索引库中的数据。

查询条件

{
  "query": {
    "multi_match": {
      "fields": ["desc","nickname"],
      "query": "yuo everyady",
      "fuzziness": "AUTO"
    }
  }
}

查询结果

{
    "took": 35,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 4.5765147,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1010",
                "_score": 4.5765147,
                "_source": {
                    "id": 1010,
                    "age": 30,
                    "username": "cc",
                    "nickname": "cc",
                    "money": 100.8,
                    "desc": " i cc you everyday",
                    "sex": 1,
                    "birthday": "1988-07-14",
                    "face": "http://www.p2pi.cn/static/img/1010_face.png"
                }
            }
        ]
    }
}

自动纠正数据,还是可以查询到指定的信息的 。

wildcard

占位符查询;查询的结果当中必定会有 占位符中的条件

  • ? :1个字符
  • *: 1个或者多个字符

查询条件

{
    "query": {
        "wildcard": {
            "desc": "好*"
        }
    }
}

查询结果

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 1.0,
                "_source": {
                    "id": 1011,
                    "age": 31,
                    "username": "petter",
                    "nickname": "皮特",
                    "money": 180.8,
                    "desc": "皮特的姓氏好像是彼得",
                    "sex": 1,
                    "birthday": "1989-08-14",
                    "face": "http://www.p2pi.cn/static/img/1011_face.png"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "id": 1005,
                    "age": 25,
                    "username": "switch",
                    "nickname": "switch游戏机",
                    "money": 155.8,
                    "desc": "好的游戏,才会有人购买,比如塞尔达",
                    "sex": 1,
                    "birthday": "1989-03-14",
                    "face": "http://www.p2pi.cn/static/img/1005_face.png"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.0,
                "_source": {
                    "id": 1004,
                    "age": 22,
                    "username": "redHat",
                    "nickname": "红帽子",
                    "money": 55.8,
                    "desc": "好的系统必须拥有稳定的系统结构",
                    "sex": 0,
                    "birthday": "1988-02-14",
                    "face": "http://www.p2pi.cn/static/img/1004_face.png"
                }
            }
        ]
    }
}

布尔查询

可以实现多重组合查询

  • must:查询必须匹配搜索条件,譬如 and
  • should:查询匹配满足一个条件,譬如 or
  • must_not:不匹配搜索条件,一个都不满足,譬如 not in

单个查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "好的",
                        "fields": ["desc","nickname"]
                    }
                },
                {
                    "term": {
                        "sex": 0
                    }
                },
                {
                    "term": {
                        "birthday": "1992-12-24"
                    }
                }
            
            ]
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc"
        ]
}

组合查询

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "desc": "好的"
                    }
                },
                {
                    "match": {
                        "nickname": "好的"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "sex": 1
                    }
                }
            ],
            "must_not":[
                {
                    "term": {
                        "birthday": "1993-01-24"
                    }
                }
            ]
            
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc",
            "birthday"
        ]
}

增加权重

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "desc": {
                            "query": "好的",
                            "boost": 2
                        }
                    }
                },
                {
                    "match": {
                        "desc": {
                            "query": "男孩",
                             "boost": 20
                        }
                    }
                }
            ]
            
        }
    },
        "_source": [
            "id",
            "sex",
            "nickname",
            "desc",
            "birthday"
        ]
}

分页查询

默认为form,size查询,但是有时不能满足需求需要自定义查询,这里默认分页查询

  • from: 要查询开始的起始记录
  • size :步长,要查询的数量,即每页大小
{
    "query": {
        "match_all": {}
    },
    "_source": [
        "id",
        "nickname",
        "age"
    ],
    "from": 0,
    "size": 10
}

Head 可视化操作

在这里插入图片描述

    "query": "好的",
                        "boost": 2
                    }
                }
            },
            {
                "match": {
                    "desc": {
                        "query": "男孩",
                         "boost": 20
                    }
                }
            }
        ]
        
    }
},
    "_source": [
        "id",
        "sex",
        "nickname",
        "desc",
        "birthday"
    ]

}


## 分页查询

默认为form,size查询,但是有时不能满足需求需要自定义查询,这里默认分页查询

- from: 要查询开始的起始记录
- size :步长,要查询的数量,即每页大小

```json
{
    "query": {
        "match_all": {}
    },
    "_source": [
        "id",
        "nickname",
        "age"
    ],
    "from": 0,
    "size": 10
}

Successful !!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值