ElasticSearch常用的几种查询方式

elasticsearch的命令行操作增删改查使用restful方式操作,总体有三种:

  1. kibana方式
  2. postman的http请求
  3. linux系统下的curl方式

本文主要介绍使用postman怎样对ES进行操作。着重介绍ES的查询方式

目录

1 term查询(精准查询)

2 math查询(分词匹配查询)

3 fuzzy查询(模糊查询)

4  wildcard(通配符查询)

5 bool查询(布尔查询)

6其他知识点 


1 term查询(精准查询)

  •  term是ES中的精准查询,不会参与ES分词查询。
http://192.168.25.128:9200/myname/_search
{
	"query":{
		"term":{
			"name":"zhangsan"
		}
	}
}


//查询结果
{
    "took": 18,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.30685282,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "3",
                "_score": 0.30685282,
                "_source": {
                    "id": "3",
                    "name": "zhangsan",
                    "age": 20,
                    "sex": 1,
                    "description": "张三是数据库工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}
  • terms 查询是term的扩展,可以支持多个vlaue匹配,只需要一个匹配就可以了。
http://192.168.25.128:9200/myname/_search
{
	"query":{
		"terms":{
			"name":["zhangsan","lisi"]
		}
	}
}


//查询结果
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.25427115,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "2",
                "_score": 0.25427115,
                "_source": {
                    "id": "2",
                    "name": "lisi",
                    "age": 12,
                    "sex": 1,
                    "description": "李四是个工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "3",
                "_score": 0.04500804,
                "_source": {
                    "id": "3",
                    "name": "zhangsan",
                    "age": 20,
                    "sex": 1,
                    "description": "张三是数据库工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
   

 

2 math查询(分词匹配查询)

match查询是按ES分词的倒排表进行查询,而keyword不会被分词,match的需要跟keyword的完全匹配可以。可以用于一般性的匹配查询。

  • match_all

match_all可以用于查询全部信息。

http://192.168.25.128:9200/myname/_search
{
	"query":{
		"match_all":{
		}
	}
}


//查询结果
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "id": "5",
                    "name": "zhangsan1",
                    "age": 25,
                    "sex": 1,
                    "description": "zhangsan",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "id": "2",
                    "name": "lisi",
                    "age": 12,
                    "sex": 1,
                    "description": "李四是个工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "id": "4",
                    "name": "马六工程师",
                    "age": 25,
                    "sex": 1,
                    "description": "马六是前端工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "id": "1",
                    "name": "wangwu",
                    "age": 25,
                    "sex": 1,
                    "description": "王五是个java开发工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "id": "3",
                    "name": "zhangsan",
                    "age": 20,
                    "sex": 1,
                    "description": "张三是数据库工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
  • match

mathc是单个字段进行分词匹配查询。

http://192.168.25.128:9200/myname/_search
{
	"query":{
		"match":{
			"name":"zhangsan"
		}
	}
}

//查询结果
{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.8784157,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 0.8784157,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "3",
                "_score": 0.30685282,
                "_source": {
                    "id": "3",
                    "name": "zhangsan",
                    "age": 20,
                    "sex": 1,
                    "description": "张三是数据库工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}
  • multi_match

multi_match是多字段进行匹配查询。

http://192.168.25.128:9200/myname/_search
{
	"query":{
		"multi_match":{
			"query":"zhangsan",
			"fields":["name","description"]
		}
	}
}

//查询结果

{
    "took": 18,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.8784157,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 0.8784157,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "5",
                "_score": 0.09415865,
                "_source": {
                    "id": "5",
                    "name": "zhangsan1",
                    "age": 25,
                    "sex": 1,
                    "description": "zhangsan",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "3",
                "_score": 0.09415865,
                "_source": {
                    "id": "3",
                    "name": "zhangsan",
                    "age": 20,
                    "sex": 1,
                    "description": "张三是数据库工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}
  • match_phrase

在执行短语匹配查询时,ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变。

http://192.168.25.128:9200/myname/_search
{
	"query":{
		"match_phrase":{
			"name":"zhangsan lisi"
		}
	}
}

//查询结果
{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.5034157,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 1.5034157,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}

3 fuzzy查询(模糊查询)

fuzzy查询可以用于纠正去拼写的问题。

fuzziness是可以允许纠正错误拼写的个数
http://192.168.25.128:9200/myname/_search
{
	"query":{
		"fuzzy":{
			"name":{
				"value":"lisss",
				"fuzziness":2
			}
				
		}
	}
}

//查询结果
{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "id": "2",
                    "name": "lisi",
                    "age": 12,
                    "sex": 1,
                    "description": "李四是个工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 0.625,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }

4  wildcard(通配符查询)

  • 通配符查询允许我们指定一个模式来匹配,而不需要指定完整的trem,匹配的方式类似于match的分词匹配查询。
  • ?将会匹配如何字符;*将会匹配零个或者多个字符。
http://192.168.25.128:9200/myname/_search
{
	"query":{
			"wildcard":{
				"name":"li*"
			}
	}
}

//查询结果
{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "id": "2",
                    "name": "lisi",
                    "age": 12,
                    "sex": 1,
                    "description": "李四是个工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 1.0,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}

 

5 bool查询(布尔查询)

bool查询本身没有查询功能,而是基于逻辑值使用前面几种查询方式进行组合查询。

  • should方式
http://192.168.25.128:9200/myname/_search
{
	"query":{
		"bool":{
			"should":[
				{ "match" : { "name":"zhangsan" }},
				{ "match" : { "name":"lisi"}}
				]
		}
	}
}

//查询结果
{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1.078072,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 1.078072,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "2",
                "_score": 0.28986934,
                "_source": {
                    "id": "2",
                    "name": "lisi",
                    "age": 12,
                    "sex": 1,
                    "description": "李四是个工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "3",
                "_score": 0.04500804,
                "_source": {
                    "id": "3",
                    "name": "zhangsan",
                    "age": 20,
                    "sex": 1,
                    "description": "张三是数据库工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}
  • must方式
http://192.168.25.128:9200/myname/_search
{
	"query":{
		"bool":{
			"must":[
				{
				"match":{
					"name":"zhangsan"
				}},
				{
				"match":{
					"name":"lisi"
				}}
				]
		}
	}
}


//查询结果
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.078072,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": 1.078072,
                "_source": {
                    "id": "6",
                    "name": "zhangsan lisi",
                    "age": 30,
                    "sex": 1,
                    "description": "zhangsan and lisi",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}
  • must_not
http://192.168.25.128:9200/myname/_search
{
	"query":{
		"bool":{
			"must_not":[
				{
				"match":{
					"name":"zhangsan"
				}},
				{
				"match":{
					"name":"lisi"
				}}
				]
		}
	}
}

//查询结果
{
    "took": 20,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "id": "5",
                    "name": "zhangsan1",
                    "age": 25,
                    "sex": 1,
                    "description": "zhangsan",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "id": "4",
                    "name": "马六工程师",
                    "age": 25,
                    "sex": 1,
                    "description": "马六是前端工程师",
                    "createTime": null,
                    "updateTime": null
                }
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "id": "1",
                    "name": "wangwu",
                    "age": 25,
                    "sex": 1,
                    "description": "王五是个java开发工程师",
                    "createTime": null,
                    "updateTime": null
                }
            }
        ]
    }
}

6其他知识点 

  • 取特定字段(_source)
  • 分页(size ,from)
  • 排序(sort)
http://192.168.25.128:9200/myname/_search

{
	"_source":["name","description"],
	"query":{
			"match":{
				"name":"lisi?"
			}
	},
	"size":2,
	"from":0,
	"sort":{
		"age":{"order":"desc"}
	}
}

//查询方式
{
    "took": 341,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": null,
        "hits": [
            {
                "_index": "myname",
                "_type": "user",
                "_id": "6",
                "_score": null,
                "_source": {
                    "name": "zhangsan lisi",
                    "description": "zhangsan and lisi"
                },
                "sort": [
                    30
                ]
            },
            {
                "_index": "myname",
                "_type": "user",
                "_id": "2",
                "_score": null,
                "_source": {
                    "name": "lisi",
                    "description": "李四是个工程师"
                },
                "sort": [
                    12
                ]
            }
        ]
    }
}

 

你可能感兴趣的:(elasticsearch)