ES 7.9.0 初探之常见操作-match

文章目录

    • 1.前缀查询(prefix)
    • 2. 短语匹配(match_phrase)
    • 3.短语前缀匹配查询(match_phrase_prefix)
    • 4.多重匹配查询(Multi-match query)
    • 5.是否匹配前缀(match_bool_prefix)

上一篇我们使用ES进行了简单的CURD操作,今天来进行一点简单的匹配查询的操作。
首先来看一下我编了哪些数据:

GET yytest/_search

得到的数据有

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "l5PudHUBTpZvBmMg9zdF",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 23,
          "birthday" : "1998-08-28"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "kZU9jnUBTpZvBmMggwYh",
        "_score" : 1.0,
        "_source" : {
          "name" : "jerry David",
          "age" : 24,
          "birthday" : "1996-03-12",
          "description" : "he likes playing LOL"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "tJU-jnUBTpZvBmMg6gZo",
        "_score" : 1.0,
        "_source" : {
          "name" : "lucy Allen",
          "age" : 23,
          "birthday" : "1997-09-22",
          "description" : "she is pretty"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "dpU8jnUBTpZvBmMgfAZR",
        "_score" : 1.0,
        "_source" : {
          "name" : "tom green",
          "age" : "23",
          "birthday" : "1995-08-18",
          "description" : "he is tall and handsome, and tony is his uncle"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "LJZNonUBTpZvBmMgSoqi",
        "_score" : 1.0,
        "_source" : {
          "name" : "tony stack",
          "age" : "34",
          "birthday" : "1986-09-21",
          "description" : "he likes playing basketball, his last name is stack"
        }
      }
    ]
  }
}

1.前缀查询(prefix)

查询语句

GET yytest/_search
{
  "query":{
    "prefix": {
      "description": "he"
    }
  }
}

这样的GET请求将会得到下面的返回结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "kZU9jnUBTpZvBmMggwYh",
        "_score" : 1.0,
        "_source" : {
          "name" : "jerry David",
          "age" : 24,
          "birthday" : "1996-03-12",
          "description" : "he likes playing LOL"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "dpU8jnUBTpZvBmMgfAZR",
        "_score" : 1.0,
        "_source" : {
          "name" : "tom green",
          "age" : "23",
          "birthday" : "1995-08-18",
          "description" : "he is tall and handsome, and tony is his uncle"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "LJZNonUBTpZvBmMgSoqi",
        "_score" : 1.0,
        "_source" : {
          "name" : "tony stack",
          "age" : "34",
          "birthday" : "1986-09-21",
          "description" : "he likes playing basketball, his last name is stack"
        }
      }
    ]
  }
}

从返回的数据中,我们可以看到,hit了3条数据。
注意:
如果的ES版本比较旧的,即7.0以前的,查询语句需要指定type,但是7.0之后的版本已经逐步废弃这一用法了。如果用以下的查询语句,即

GET yytest/_doc/_search
{
  "query":{
    "prefix": {
      "description": "he"
    }
  }
}

也能够返回数据,但是最上面会显示如下的内容
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
因此我们不要再指定type了。

2. 短语匹配(match_phrase)

查询description中的短语包括“likes playing”
查询的语句是

GET yytest/_search
{
  "query": {
    "match_phrase": {
      "description": "likes playing"
    }
  }
}

返回的结果是

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.0482664,
    "hits" : [
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "kZU9jnUBTpZvBmMggwYh",
        "_score" : 2.0482664,
        "_source" : {
          "name" : "jerry David",
          "age" : 24,
          "birthday" : "1996-03-12",
          "description" : "he likes playing LOL"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "LJZNonUBTpZvBmMgSoqi",
        "_score" : 1.4778953,
        "_source" : {
          "name" : "tony stack",
          "age" : "34",
          "birthday" : "1986-09-21",
          "description" : "he likes playing basketball, his last name is stack"
        }
      }
    ]
  }
}

3.短语前缀匹配查询(match_phrase_prefix)

短语前缀匹配查询,即经过分词器分词之后,最后一个词作为搜素的前缀。举例来说

GET yytest/_search
{
  "query": {
    "match_phrase_prefix": {
      "description": {
        "max_expansions": 10,
        "query": "he likes play"
      }
    }
  }
}

这里的就是搜索短语,短语的前两个单词是“he likes ”,然后以第三个单词play作为前缀条件,继续搜索。返回的结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.3848,
    "hits" : [
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "kZU9jnUBTpZvBmMggwYh",
        "_score" : 2.3848,
        "_source" : {
          "name" : "jerry David",
          "age" : 24,
          "birthday" : "1996-03-12",
          "description" : "he likes playing LOL"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "LJZNonUBTpZvBmMgSoqi",
        "_score" : 1.720716,
        "_source" : {
          "name" : "tony stack",
          "age" : "34",
          "birthday" : "1986-09-21",
          "description" : "he likes playing basketball, his last name is stack"
        }
      }
    ]
  }
}

可以看到,匹配了两条数据。
然后“max_expansions"用来限制匹配道的文档的数量,默认值为50。即如果素以库中有20条数据,限制了max_expansions为10,则会按存储顺序只返回前10条数据。不过这里我试了一下,貌似有点问题,当我将其限制为1条的时候,还是会查处两条数据,即我当前索引库的所有数据。
max_expansions讲解了有可能的原因,但是我测试的结果不符合预期,有懂的大佬可以指点我一下。

4.多重匹配查询(Multi-match query)

多重匹配查询是匹配查询多个字段,例如查询name和description字段中包含tony的文档,即

GET yytest/_search
{
  "query": {
    "multi_match": {
      "query": "tony",
      "fields": ["name","description"]
    }
  }
}

返回的结果是

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.567127,
    "hits" : [
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "LJZNonUBTpZvBmMgSoqi",
        "_score" : 1.567127,
        "_source" : {
          "name" : "tony stack",
          "age" : "34",
          "birthday" : "1986-09-21",
          "description" : "he likes playing basketball, his last name is stack"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "dpU8jnUBTpZvBmMgfAZR",
        "_score" : 1.1083853,
        "_source" : {
          "name" : "tom green",
          "age" : "23",
          "birthday" : "1995-08-18",
          "description" : "he is tall and handsome, and tony is his uncle"
        }
      }
    ]
  }
}

可以看到,这里包含了两个文档,其中是那么字段中包含tony的文档LJZNonUBTpZvBmMgSoqi,和描述中国包含tony的文档dpU8jnUBTpZvBmMgfAZR。

5.是否匹配前缀(match_bool_prefix)

match_bool_prefix与match_phrase_prefix的区别在于match_bool_prefix分词后,不管单词的前后顺序,即如果匹配“he likes”,那么“likes he”,“he“和"likes"都会匹配到。

GET yytest/_search
{
  "query":{
    "match_bool_prefix":{
      "description":"tony stack"
    }
  }
}

返回的结果是

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.1083853,
    "hits" : [
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "dpU8jnUBTpZvBmMgfAZR",
        "_score" : 1.1083853,
        "_source" : {
          "name" : "tom green",
          "age" : "23",
          "birthday" : "1995-08-18",
          "description" : "he is tall and handsome, and tony is his uncle"
        }
      },
      {
        "_index" : "yytest",
        "_type" : "_doc",
        "_id" : "LJZNonUBTpZvBmMgSoqi",
        "_score" : 1.0,
        "_source" : {
          "name" : "tony stack",
          "age" : "34",
          "birthday" : "1986-09-21",
          "description" : "he likes playing basketball, his last name is stack"
        }
      }
    ]
  }
}

最后给出官网的地址ES 匹配,大家可以自行查阅。

你可能感兴趣的:(elasticsearch,java,elasticsearch,es)