ES的常用查询

1 数据准备

1.1 创建索引和新增数据

先新增一条数据,新增数据时会自动创建索引 test_standard_analyzer。

PUT /test_standard_analyzer/_doc/1
{
  "remark": "This is a test doc"
}

PUT /test_standard_analyzer/_doc/2
{
  "remark": "This is an apple"
}

然后查询一下。

GET test_standard_analyzer/_search
{
  "query": {
    "match_all": {}
  }
}

查询结果如下所示。

    "hits" : [
      {
        "_index" : "test_standard_analyzer",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "remark" : "This is a test doc"
        }
      },
      {
        "_index" : "test_standard_analyzer",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "remark" : "This is an apple"
        }
      }
    ]

1.2 测试分词

没指定es分词器时,es会使用默认分词器-standard。测试下分词效果。

 POST test_standard_analyzer/_analyze
{
  "field": "remark",
  "text": "This is a test doc"
}

# 或

 POST test_standard_analyzer/_analyze
{
  "analyzer": "standard",
  "text": "This is a test doc"
}

分词结果如下所示。

{
  "tokens" : [
    {
      "token" : "this",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "is",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "",
      "position" : 1
    },
    {
      "token" : "a",
      "start_offset" : 8,
      "end_offset" : 9,
      "type" : "",
      "position" : 2
    },
    {
      "token" : "test",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "",
      "position" : 3
    },
    {
      "token" : "doc",
      "start_offset" : 15,
      "end_offset" : 18,
      "type" : "",
      "position" : 4
    }
  ]
}

2 ES查询

2.1 match

match查询会将查询条件进行分词。命中数据的条件:匹配到查询条件的其中一个分词即可。

以下查询将命中数据,查询条件被分词为“b”和“doc”。

GET test_standard_analyzer/_search
{
  "query": {
    "match": {
      "remark":"b doc"
    }
  }
}

2.2 match_phrase

match_phrase查询会将查询条件进行分词。

命中数据的条件:(1)查询条件的所有分词都需要匹配到,(2)相对顺序还要一致,(3)默认(slop=0或者未设置该值)查询条件的分词在es数据中是连续的。

2.2.1 查询条件的分词在es数据中需要是连续的

(1)命中数据

GET test_standard_analyzer/_search
{
  "query": {
    "match_phrase": {
      "remark":"a test doc"
    }
  }
}

(2)未命中数据

以下查询未命中数据,因为查询条件的分词在es数据中不连续,中间还间隔一个“test”。

GET test_standard_analyzer/_search
{
  "query": {
    "match_phrase": {
      "remark":"a doc"
    }
  }
}

2.2.2 查询条件的分词在es数据中不需要是连续的

slop 参数用于指定中间可省略几个词语。slop > 0时,查询条件的分词在es数据中可以不连续。

因此以下查询将命中数据。

GET test_standard_analyzer/_search
{
  "query": {
    "match_phrase": {
      "remark":{
        "query": "a doc",
        "slop": 1
      }
    }
  }
}

2.3 term

term查询不会对查询条件进行分词,而是直接拿查询条件作为一个词去和倒排索引进行匹配。匹配到了,则命中了es的数据,否则未命中es数据。下面是具体的查询案例。

2.3.1 未命中数据

因为倒排索引中没有 “This is a test doc” 这个词。

GET test_standard_analyzer/_search
{
  "query": {
    "term": {
      "remark":"This is a test doc"
    }
  }
}

2.3.2 命中了数据

因为此查询匹配到了倒排索引中的词-“doc”。

GET test_standard_analyzer/_search
{
  "query": {
    "term": {
      "remark":"doc"
    }
  }
}

2.4 terms

terms查询不会对查询条件进行分词,而是直接拿查询条件作为词去和倒排索引进行匹配。查询条件中至少一个词匹配到了,则命中了es的数据。terms查询相当于对多个term查询结果取并集,即取所有词的匹配结果的并集。下面是具体的查询案例。

2.4.1 命中多条数据

查询语句

GET test_standard_analyzer/_search
{
  "query": {
    "terms": {
      "remark": ["doc", "apple"]
    }
  }
}

查询结果

    "hits" : [
      {
        "_index" : "test_standard_analyzer",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "remark" : "This is a test doc"
        }
      },
      {
        "_index" : "test_standard_analyzer",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "remark" : "This is an apple"
        }
      }
    ]

2.4.2 命中一条数据

查询语句

GET test_standard_analyzer/_search
{
  "query": {
    "terms": {
      "remark": ["haha", "apple"]
    }
  }
}

查询结果

    "hits" : [
      {
        "_index" : "test_standard_analyzer",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "remark" : "This is an apple"
        }
      }
    ]

2.4.3 未命中数据

GET test_standard_analyzer/_search
{
  "query": {
    "terms": {
      "remark": ["tom", "jack"]
    }
  }
}

3 参考文献

(1)【ES知识】ES基础查询语法一览

你可能感兴趣的:(ES,elasticsearch,大数据,搜索引擎,es查询)