商道如狼道 发表于 2024-7-21 18:17:45

Elasticsearch 入门实战(8)--REST API 使用二(Search API)

本文继承上文(Elasticsearch 入门实战(3)--REST API 使用一(CAT,Index,Document,Ingest API))先容 Elasticsearch REST API,相关的情况及软件信息如下:CentOS 7.6.1810、Elasticsearch 8.13.4。
1、Search APIs

1.1、Count API(查询文档数目)

语法:
GET /<target>/_count样例:
curl -X GET 'http://10.49.196.33:9200/poet-index/_count'             #查询该索引的所有文档数量

curl -X GET 'http://10.49.196.33:9200/poet-index/_count?q=name:杜甫' #通过 Lucene 查询语法指定条件

curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.33:9200/poet-index/_count' -d ' #通过 "Query DSL" 指定条件
{
"query": {
    "term": {
      "name.keyword": {
      "value": "杜甫"
      }
    }
}
}'1.2、Search API(查询文档)

语法:
GET /<target>/_search
GET /_search
POST /<target>/_search
POST /_search1.2.1、query

1.2.1.1、term/terms 查询

term 查询不会对输入的内容进行分词处理,而是作为一个团体来查询。
A、查询单个词
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "term": {
      "name": {
      "value": "李白"
      }
    }
}
}'B、查询多个词
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "terms": {
      "name": ["李白", "杜甫"]
    }
}
}'1.2.1.2、range 查询

按照范围查询。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "range": {
      "age": {
      "gte": 20,
      "lte": 35
      }
    }
}
}'1.2.1.3、exists 查询

查询对应字段不为空的数据。
curl -X GET -H 'Content-Type:application/json' 'http://10.1.101.64:9200/poet-index/_search' -d '
{
"query": {
    "exists": {
      "field": "poems"
    }
}
}'1.2.1.4、match 相关查询

A、match
对输入的内容进行分词处理,再根据分词查询。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "match": {
      "success": "理想主义"
    }
},
"from": 0,
"size": 10,
"sort": [{
    "name": {
      "order": "asc"
    }
}]
}'B、multi_match
多字段进行匹配。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "multi_match": {
      "query": "太白",
      "fields": ["about", "success"]
    }
}
}'C、match_phrase
类似 match,必要满足以下条件:
1.文档的分词列表要包含所有的搜索分词列表
2.搜索分词次序要和文档分词次序一致
3.slop 参数控制着匹配到的文档分词最大间距,默以为1(匹配到分词要紧挨着)
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "match_phrase": {
      "success": "文学作家"
    }
}
}'D、match_all
查询所有文档。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "match_all": {
   }
}
}'不加请求体,也是一样的效果,查询所有文档。
curl -X GET 'http://10.49.196.11:9200/poet-index/_search'E、match_none
与 match_all 相反,返回 0 个文档。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "match_none": {
   }
}
}'1.2.1.5、query_string 查询

query_string 可以同时实现前面几种查询方法。
A、类似 match
curl -X GET-H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "query_string": {
      "default_field": "success",
      "query": "古典文学"
    }
}
}'B、类似 mulit_match
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "query_string": {
      "query": "古典文学",
      "fields": ["about", "success"]
    }
}
}'C、类似 match_phrase
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "query_string": {
      "default_field": "success",
      "query": "\"古典文学\""
    }
}
}'D、带运算符查询,运算符两边的词不再分词
1、查询同时包含 ”文学“ 和 ”巨大“ 的文档
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "query_string": {
      "default_field": "success",
      "query": "文学 AND 伟大"
    }
}
}'或
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "query_string": {
      "fields": ["success"],
      "query": "文学 伟大",
      "default_operator": "AND"
    }
}
}'2、查询 name 或 success 字段包含"文学"和"巨大"这两个单词,或者包含"李白"这个单词的文档。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "query_string": {
      "query": "(文学 AND 伟大) OR 李白",
      "fields": ["name", "success"]
    }
}
}'1.2.1.6、simple_query_string 查询

类似 query_string,重要区别如下:
1、不支持AND OR NOT ,会当做字符处理;使用 + 代替 AND,| 代替OR,- 代替 NOT
2、会忽略错误的语法
查询同时包含 ”文学“ 和 ”巨大“ 的文档:
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "simple_query_string": {
      "fields": ["success"],
      "query": "文学 + 伟大"
    }
}
}'1.2.1.7、fuzzy 查询

模糊查询时使用的参数:
fuzziness
允许的最大编辑间隔,默认不开启模糊查询,相称于 fuzziness=0。支持的格式
1、可以是数字(0、1、2)代表固定的最大编辑间隔
2、主动模式,AUTO:,
    查询词长度在 [0-low)范围内编辑间隔为 0(即强匹配)
    查询词长度在 [low, high) 范围内允许编辑 1 次
    查询词长度 >high 允许编辑 2 次
prefix_length
控制两个字符串匹配的最小相同的前缀巨细,也就是前 n 个字符不允许编辑,必须与查询词相同,默认是 0,大于 0 时可以明显提升查询性能
max_expansions
产生的最大模糊选项
transpositions
相邻位置字符交换是否算作 1 次编辑间隔,全文查询不支持该参数
A、全文查询时使用模糊参数
先分词再计算模糊选项。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "match": {
       "success": {
         "query": "古典文化",
         "fuzziness": 1,
         "prefix_length": 0,
         "max_expansions": 5
       }
    }
}
}'B、使用 fuzzy query
对输入不分词,直接计算模糊选项。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "fuzzy": {
      "success": {
      "value": "理想",
      "fuzziness": 1,
      "prefix_length": 0,
      "transpositions": true
      }
    }
}
}'1.2.1.8、wildcard 查询

wildcard 查询类似 SQL 语句中的 like;? 匹配一个字符,* 匹配多个字符。对于使用 wildcard 查询的字段发起字段类型设为 wildcard 类型。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "wildcard": {
      "name": "李*"
    }
}
}'1.2.1.9、bool 查询

组合查询使用 bool 来组合多个查询条件。
条件阐明must同时满足should满足此中任意一个must_not同时不满足filter过滤搜索,不计算得分A、查询 success 包含 “思想” 且 age 在 之间的文档:
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "bool": {
      "must": [{
      "simple_query_string": {
          "query": "思想",
          "fields": ["success"]
      }
      }, {
      "range": {
          "age": {
            "gte": 20,
            "lte": 40
          }
      }
      }]
    }
}
}'B、过滤出 success 包含 “思想” 且 age 在 之间的文档,不计算得分:
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "bool": {
      "filter": [{
      "simple_query_string": {
          "query": "思想",
          "fields": ["success"]
      }
      }, {
      "range": {
          "age": {
            "gte": 20,
            "lte": 40
          }
      }
      }]
    }
}
}'1.2.2、aggs 查询

聚合查询类似 SQL 中的 group by 分组查询。
A、求和,类似 select sum(age) from poet-index
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"aggs": {
    "age_sum": {
      "sum": {
      "field": "age"
      }
    }
}
}'B、类似 select count distinct(age) from poet-index
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/test-index/_search' -d '
{
"aggs": {
    "age_count": {
      "cardinality": {
      "field": "age"
      }
    }
}
}'C、数目、最大、最小、平均、求和
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"aggs": {
    "age_stats": {
      "stats": {
      "field": "age"
      }
    }
},
"size": 0
}'D、类似 select name,count(*) from poet-index group by name
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"aggs": {
    "name_terms": {
      "terms": {
      "field": "name"
      }
    }
},
"size": 0
}'E、类似 select name,age, count(*) from poet-index group by name,age
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"aggs": {
    "name_terms": {
      "terms": {
      "field": "name"
      },
      "aggs": {
      "age_terms": {
          "terms": {
            "field": "age"
          }
      }
      }
    }
},
"size": 0
}'F、类似 select avg(age) from poet-indexwhere name='李白'
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "bool": {
      "filter": {
      "term": {
          "name": "李白"
      }
      }
    }
},
"aggs": {
    "age_avg": {
      "avg": {
      "field": "age"
      }
    }
},
"size": 0
}'1.2.3、suggest 查询

如果希望 Elasticsearch 能够根据我们的搜索内容给一些保举的搜索选项,可以使用保举搜索。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"suggest": {
    "success_suggest": {
      "text": "思考",
      "term": {
      "field": "success",
      "analyzer": "ik_max_word",
      "suggest_mode": "always",
      "min_word_length":2
      }
    }
}
}'保举模式 suggest_mode:
 保举模式阐明popular保举词频更高的一些搜索missing当没有要搜索的结果的时间才保举always无论什么情况下都进行保举1.2.4、highlight

对搜索结果中的关键字高亮显示。
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_search' -d '
{
"query": {
    "match": {
      "success": "思想"
    }
},
"highlight": {
    "pre_tags": "<span color='red'>",
    "post_tags": "</span>",      
    "fields": {            
      "success": {}
    }
}
}' 

 
详细的 Elasticsearch REST API 使用阐明,请参考官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Elasticsearch 入门实战(8)--REST API 使用二(Search API)