elasticsearch中的查询

2021-05-27  本文已影响0人  dark68

1.普通查询

GET /products/_doc/_search
{
  "query":{
    "match":{
      "name":"HUAWEI"
    }
  }
}

返回的结果:

{
  "took" : 948,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 14.68131,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200039",
        "_score" : 14.68131,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200039,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 14 16G 512G  ",
          "name" : "HUAWEi Mate Book 14",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 14.68131,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      }
    ]
  }
}

2.查询指定字段:查询name和price两个字段

GET /products/_doc/_search
{
  "query":{
    "match":{
      "name":"HUAWEI"
    }
  },
  "_source":["name","price"]
}

查询出的结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 14.68131,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200039",
        "_score" : 14.68131,
        "_source" : {
          "price" : 9090,
          "name" : "HUAWEi Mate Book 14"
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 14.68131,
        "_source" : {
          "price" : 9090,
          "name" : "HUAWEi Mate Book 13"
        }
      }
    ]
  }
}

3.排序:默认是根据_score匹配分值进行降序排序的,但如果我们指定一个字段进行asc或者desc排序呢?这里我使用price来进行降序

GET /products/_doc/_search
{
  "query":{
    "match":{
      "name":"Book"
    }
  },
  "_source":["name","price"],
  "sort":[
    {
      "price":{
        "order":"desc"
      }
    }  
  ]
}

查询出的结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200039",
        "_score" : null,
        "_source" : {
          "price" : 9090,
          "name" : "HUAWEi Mate Book 14"
        },
        "sort" : [
          9090.0
        ]
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : null,
        "_source" : {
          "price" : 9090,
          "name" : "HUAWEi Mate Book 13"
        },
        "sort" : [
          9090.0
        ]
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : null,
        "_source" : {
          "price" : 9080,
          "name" : "IPhone Mate Book 13"
        },
        "sort" : [
          9080.0
        ]
      }
    ]
  }
}

4.分页查询:添加了from和size属性,其中from是指分页的起始索引,size是指分页容量

GET /products/_doc/_search
{
  "query":{
    "match":{
      "name":"Book"
    }
  },
  "_source":["name","price"],
  "sort":[
    {
      "price":{
        "order":"desc"
      }
    }  
  ],
  "from":1,
  "size":1
}

查询出的结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : null,
        "_source" : {
          "price" : 9090,
          "name" : "HUAWEi Mate Book 13"
        },
        "sort" : [
          9090.0
        ]
      }
    ]
  }
}

5.布尔查询,多条件查询

其中bool代表此查询为布尔查询,也就是多条件查询
must(and),所有的条件都要符合,相当于where id=1 and name=xxx
should(or),满足一个条件即可,相当于 where id=1 or name=xxx
must_not(!=),所有条件必须满足不等于,相当于where id!=1 and name!=xxx

must查询

GET /products/_doc/_search
{
  "query":{
    "bool":{
      "must":[
        {
          "match":{
            "name":"Book"
          }
        },{
          "match":{
            "shop_id":1
          }
        }  
      ]
    }
  }
}

查询出的结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 14.91694,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200039",
        "_score" : 14.91694,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200039,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 14 16G 512G  ",
          "name" : "HUAWEi Mate Book 14",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 14.91694,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : 14.91694,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200041,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "IPhone Mate Book 13",
          "price" : 9080,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      }
    ]
  }
}

should查询

GET /products/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "name":"Book"
          }
        },{
          "match":{
            "shop_id":1
          }
        }  
      ]
    }
  }
}

查询出的结果:

{
  "took" : 42,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.744874,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 1.744874,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 8888,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : 1.744874,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200041,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "IPhone Mate Book 13",
          "price" : 5499,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200043",
        "_score" : 1.0,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200043,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "MateBook 16G 512G  ",
          "name" : "MateBook X",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200042",
        "_score" : 1.0,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200042,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "OPPO 40s  16G 512G  ",
          "name" : "OPPO 40s",
          "price" : 13880,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      }
    ]
  }
}

must_not查询

GET /products/_doc/_search
{
  "query":{
    "bool":{
      "must_not":[
        {
          "match":{
            "name":"Book"
          }
        },{
          "match":{
            "shop_id":1
          }
        }  
      ]
    }
  }
}

查询出的结果:(因为没有相关数据,所以返回的是空)

{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

6. 过滤查询:(查询name带有Book或者shop_id等于1,并筛选出1000<price<=6000的文档)

#修改旧数据价格操作未展示
GET /products/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "name":"Book"
          }
        },{
          "match":{
            "shop_id":1
          }
        }  
      ],
      "filter":{
        "range":{
          "price":{
            "gt":1000,
            "lte":6000
          }
        }
      }
    }
  }
}

查询出的结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.744874,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : 0.744874,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 5499,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      }
    ]
  }
}

7.精准查询

1.term:通过倒排索引指定的词条进行精确的查找,配合keyword类型的字段,都不分词,直接匹配
2.match:查询之前会通过分词器解析,解析后再进行查询

#查询一
GET /products/_doc/_search
{
  "query":{
    "term":{
      "name":"Book"
    }
  }
}

#查询二
GET /products/_doc/_search
{
  "query":{
    "term":{
      "name":"Phone Mate Book 13"
    }
  }
}

#查询三
GET /products/_doc/_search
{
  "query":{
    "match":{
      "name":"Book"
    }
  }
}

查询出的结果:(查询一、二都为空,只有查询三结果如下)

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 13.655978,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200039",
        "_score" : 13.655978,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200039,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 14 16G 512G  ",
          "name" : "HUAWEi Mate Book 14",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : 13.655978,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200041,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "IPhone Mate Book 13",
          "price" : 9080,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 13.655978,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 5499,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      }
    ]
  }
}

通过GET /products可以查看到name字段的类型为text


image.png

原因:
查询一:无法精准匹配到name=“Book”的字段
查询二:由于字段被分词器解析了,词库中没有 “Phone Mate Book 13” ,所以也无法匹配
两个类型:text(会被分词器解析)和keyword(不会被分词器解析)

精准匹配数字

GET /products/_doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "term":{
            "price":5499
          }
        },
        {
          "term":{
            "price":9080
          }
        }
      ]
    }
  }
}

查询出的结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : 1.0,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200041,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "IPhone Mate Book 13",
          "price" : 9080,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 1.0,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 5499,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        }
      }
    ]
  }
}

8.高亮查询

GET /products/_doc/_search
{
  "query":{
    "match":{
      "name":"Book"
    }
  },
  "highlight":{
    "fields":{
      "name":{}
    }
  }
}

查询出的结果:(可以看到多了<em>标签)

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 13.655978,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200039",
        "_score" : 13.655978,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200039,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 14 16G 512G  ",
          "name" : "HUAWEi Mate Book 14",
          "price" : 9090,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        },
        "highlight" : {
          "name" : [
            "HUAWEi Mate <em>Book</em> 14"
          ]
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200041",
        "_score" : 13.655978,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200041,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "IPhone Mate Book 13",
          "price" : 9080,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        },
        "highlight" : {
          "name" : [
            "IPhone Mate <em>Book</em> 13"
          ]
        }
      },
      {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "200040",
        "_score" : 13.655978,
        "_source" : {
          "brand_id" : 1,
          "create_time" : "2021-05-21T00:00:00Z",
          "id" : 200040,
          "last_time" : "2021-05-21T00:00:00Z",
          "long_name" : "HUAWEi Mate Book 13 16G 512G  ",
          "name" : "HUAWEi Mate Book 13",
          "price" : 5499,
          "review_count" : 11111,
          "shop_id" : 1,
          "sold_count" : 12345,
          "status" : 1,
          "three_category_id" : 3
        },
        "highlight" : {
          "name" : [
            "HUAWEi Mate <em>Book</em> 13"
          ]
        }
      }
    ]
  }
}
上一篇下一篇

猜你喜欢

热点阅读