Elasticsearch基本语法

match和match_phrase区别

match: 索引中只要有任意一个匹配拆分后词就可以出现在结果中,只是匹配度越高的排越前面

match_phrase: 索引中必须同时匹配拆分后词就可以出现在结果中

ex:

GET /product_index/product/_search
{
  "query": {
    "match_phrase": {
      "product_name": "PHILIPS toothbrush"
    }
  }
}

product_name必须同时包含PHILIPS和toothbrush才会返回。

match的另一些用法

满足分词结果中所有的词,而不是像上面,任意一个就可以的。

GET /product_index/product/_search
{
  "query": {
    "match": {
      "product_name": {
        "query": "PHILIPS toothbrush",
        "operator": "and"
      }
     }
   }
}

 只要命中50%的分词就返回

GET /test_index/test/_search
{
  "query": {
    "match": {
      "product_name": {
        "query": "java 程序员 书 推荐",
        "minimum_should_match": "50%"
      }
    }
  }
}

multi_match: 查询a和b字段中,只要有c关键字的就出现

GET /test_index/test/_search
{
  "query": {
    "multi_match": {
      "query": "c",
      "fields": [
        "a",
        "b"
      ]
    }
  }
}

multi_match 跨多个 field 查询,表示查询分词必须出现在相同字段中

GET /product_index/product/_search
{
  "query": {
    "multi_match": {
      "query": "PHILIPS toothbrush",
      "type": "cross_fields",
      "operator": "and",
      "fields": [
        "product_name",
        "product_desc"
      ]
    }
  }
}

match_phrase + slop

  • 在说 slop 的用法之前,需要先说明原数据是:大吉大利,被分词后至少有:大 吉 大 利 四个 term。
  • match_phrase 的用法我们上面说了,按理说查询的词必须完全匹配才能查询到,吉利 很明显是不完全匹配的。
  • 但是有时候我们就是要这种不完全匹配,只要求他们尽可能靠谱,中间有几个单词是没啥问题的,那就可以用到 slop。slop = 2 表示中间如果间隔 2 个单词以内也算是匹配的结果()。
  • 实也不能称作间隔,应该说是移位,查询的关键字分词后移动多少位可以跟 doc 内容匹配,移动的次数就是 slop。所以 吉利 其实也是可以匹配到 doc 的,只是 slop = 1 才行。
    GET /product_index/product/_search
    {
      "query": {
        "match_phrase": {
          "product_name" : {
              "query" : "吉利",
              "slop" : 1
          }
        }
      }
    }

 

range用法

range用于查询数值,时间区间

GET /product_index/product/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 30.00
      }
    }
  }
}

猜你喜欢

转载自www.cnblogs.com/showtime813/p/9945440.html