美文网首页
999 - Elasticsearch Query DSL 03

999 - Elasticsearch Query DSL 03

作者: 歌哥居士 | 来源:发表于2019-04-30 06:49 被阅读0次

    Match Query

    字符串类型

    GET /examples/_search
    {
      "query": {
        "match": {
          "address": "Jiangsu"
        }
      }
    }
    

    数字类型

    GET /examples/_search
    {
      "query": {
        "match": {
          "age": 22
        }
      }
    }
    

    日期类型:虽然像字符串,但其实是日期,可以用GET /examples/_mapping查看每个字段的类型。

    GET /examples/_search
    {
      "query": {
        "match": {
          "birth": "1997-03-26"
        }
      }
    }
    

    operator默认or,所以结果含有China或Xuzhou

    operator设为and,结果就需要同时含有China和Xuzhou

    但不等同于match_phrase,因为match中设operator为and时,仅表示都要满足,例如China Xuzhou可以查出结果为China Jiangsu Xuzhou的,但match_phrase则必须满足单词为China Xuzhou

    # operator默认or
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": "China Xuzhou"
        }
      }
    }
    
    # operator设为and
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou",
            "operator": "and"
          }
        }
      }
    }
    # 查不到结果
    GET /examples/_search
    {
      "query": {
        "match_phrase": {
          "address": "China Xuzhou"
        }
      }
    }
    

    minimum_should_match控制需要满足条件的个数,默认为1。

    # 只需满足China、Xuzhou两者之一
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou", 
            "operator": "or"
          }
        }
      }
    }
    
    # 需要满足两个子句
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou", 
            "operator": "or",
            "minimum_should_match": 2
          }
        }
      }
    }
    
    # 在operator为and的情况下,表示需要满足有两个China Xuzhou。
    # 在这里就查不到数据了
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou", 
            "operator": "and",
            "minimum_should_match": 2
          }
        }
      }
    }
    
    # 75%的子句需要被匹配。2 * 75% = 1.5,向下取整=1,所以只需要匹配一个。
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou",
            "minimum_should_match": "75%"
          }
        }
      }
    }
    
    # 25%的子句可以被忽略。2 * 25% = 0.5,向下取整=0,2-0=2,所以需要匹配2个。
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou",
            "minimum_should_match": "-25%"
          }
        }
      }
    }
    
    # 当小于等于1时,条件都是必须的;大于1时则需要满足100%个。
    GET /examples/_search
    {
      "query": {
        "match": {
          "address": {
            "query": "China Xuzhou",
            "minimum_should_match": "1 < 100%"
          }
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:999 - Elasticsearch Query DSL 03

          本文链接:https://www.haomeiwen.com/subject/zjnpnqtx.html