elasticsearch 搜索语句入门

elasticsearch 通过_search 接口提供查询支持

碰到过这样奇葩的问题。是不是没有安装ik分词器就无法进行中文模糊匹配查询? 答案是当然支持 elasticsearch 本来就是一个搜索引擎没有语言限制 没有专门为english定制。接下来将在wildcard 和 match 查询的区别中为大家指明

 search 结构体如下

{
"query": {
"match": {
"xxxx": "广州"
}
},
"size": 3,
"aggs": {
"NAME": {
"AGG_TYPE": {}
}
},
"from": 0,
"sort": [
{
"FIELD": {
"order": "desc"
}
}
],
"_source": "{field}"
}

常用的一级字段 有 query 查询体   size 返回数目 aggs 聚合  from 翻页   sort 排序  _source 指定返回字段

下面主要从介绍query 讲起

1. match_all 查询

值得是匹配_source 中所有字段进行倒排索引查询  就是 select * from table;  without any condition;

2.match 正经的模糊匹配查询   select * from table  where filed like ‘%xxx%’;

{
"query": {
"match": {
"msg" : "广州 大世界"
}
}
,"size": 1
}

match 查询会将查询语句先进行分词,分成的词 如 广 州 大 世 界 五个查询默认是or 的关系 初非你手动指定operator  为 and , 或者指定要最小命中的词的个数 minimum_should_match

{
"query": {
"match": {
"body.msg" : {"query" : "广州 世界",
"operator": "and"
}
}
}
,"size": 1,
"explain": true
}

3. match 短语匹配,默认没有中文分词

{
"query": {
"match_pharse": {
"msg" : "广州 大世界"
}
}
,"size": 1
}

4. multi_match 多字段匹配

{
"query": {
"multi_match": {

"fields": ["msg","header"],
"query": "广州"

}
}
,"size": 1,
"explain": true
}

可以转换为多字段like 相同内容, 同样也支持operator 为 and , 或者指定要最小命中的词的个数 minimum_should_match
{
"query": {
"bool": {
"should": [
{ "match": { "title": "quick brown fox" }},
{ "match": { "title.original": "quick brown fox" }},
{ "match": { "title.shingles": "quick brown fox" }}
]
}
}
}

 5. term 查询 词匹配 查询 https://www.elastic.co/guide/en/elasticsearch/reference/6.6/query-dsl-common-terms-query.html#query-dsl-common-terms-query

term 查询可以将 分词结果分为高频词和低频词,进行区别查询

原文地址:https://www.cnblogs.com/leleyao/p/13416486.html