minimum_should_match

今天药忘吃喽~ 2022-11-06 05:53 194阅读 0赞
  • https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl-minimum-should-match.html
  • https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
  • https://www.elastic.co/guide/cn/elasticsearch/guide/current/_how_match_uses_bool.html

  • https://www.elastic.co/guide/en/elasticsearch/reference/7.x/full-text-queries.html


1 测试数据

  1. ### 删除
  2. DELETE /test_idx_2
  3. ### 查询
  4. GET /test_idx_2/_search
  5. ### 批量插入
  6. POST _bulk
  7. {"index":{"_index":"test_idx_2","_id":1}}
  8. {"title":"a"}
  9. {"index":{"_index":"test_idx_2","_id":2}}
  10. {"title":"a b"}
  11. {"index":{"_index":"test_idx_2","_id":3}}
  12. {"title":"a b c"}
  13. {"index":{"_index":"test_idx_2","_id":4}}
  14. {"title":"a b c d"}
  15. {"index":{"_index":"test_idx_2","_id":5}}
  16. {"title":"a b c d e"}

2 使用

可以取值:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl-minimum-should-match.html

  1. {
  2. "query":{
  3. "match":{
  4. "字段名":{
  5. "query":"查询内容",
  6. "operator":"or",
  7. "minimum_should_match":"参数"
  8. }
  9. }
  10. }
  11. }

插入测试数据

  1. ### 删除
  2. DELETE /test_idx_2
  3. ### 查询
  4. GET /test_idx_2/_search
  5. ### 批量插入
  6. POST _bulk
  7. {"index":{"_index":"test_idx_2","_id":1}}
  8. {"title":"a"}
  9. {"index":{"_index":"test_idx_2","_id":2}}
  10. {"title":"a b"}
  11. {"index":{"_index":"test_idx_2","_id":3}}
  12. {"title":"a b c"}
  13. {"index":{"_index":"test_idx_2","_id":4}}
  14. {"title":"a b c d"}
  15. {"index":{"_index":"test_idx_2","_id":5}}
  16. {"title":"a b c d e"}

“operator”:”or”,匹配到一个词即可查询到,所以这里所有的文档都能匹配到

  1. ### 全部匹配,5个
  2. POST /test_idx_2/_search
  3. {
  4. "query": {
  5. "match": {
  6. "title": {
  7. "query": "a b c d e",
  8. "operator": "or"
  9. }
  10. }
  11. }
  12. }

“minimum_should_match”: “75%”,查询条件被分为5个词,5*75%=3.75,向下取整为3,也就是查询的文档分词后需要匹配3个词才可以

  1. POST /test_idx_2/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "a b c d e",
  7. "operator": "or",
  8. "minimum_should_match": "75%"
  9. }
  10. }
  11. }
  12. }

“minimum_should_match”:”-25%”,逆向匹配和正向匹配相反,比如我们可以近似理解为-25%和75%表示的是一个意思,但是有些小小的差异。查询条件被分为5个词,逆向匹配-25%,5*25%=1.25,取整是1,5-1=4,即要匹配4个词。

  1. POST /test_idx_2/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "a b c d e",
  7. "operator": "or",
  8. "minimum_should_match": "-25%"
  9. }
  10. }
  11. }
  12. }

match并不去看分词后的顺序,只要能匹配上即可

  1. ### 分词后的内容是无顺序的
  2. POST /test_idx_2/_search
  3. {
  4. "query": {
  5. "match": {
  6. "title": {
  7. "query": "a b c",
  8. "operator": "and"
  9. }
  10. }
  11. }
  12. }
  13. POST /test_idx_2/_search
  14. {
  15. "query": {
  16. "match": {
  17. "title": {
  18. "query": "c b a",
  19. "operator": "and"
  20. }
  21. }
  22. }
  23. }

https://blog.csdn.net/xiao_jun_0820/article/details/51095521

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读