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
                ]
            }
        ]
    }
}

 

### 回答1: 要实现 Elasticsearch 多索引联合查询,可以使用 Elasticsearch 的多索引查询功能。具体来说,可以使用 Elasticsearch 的 Multi-Search API 进行多个查询操作,然后将结果合并起来返回给用户。 以下是一个简单的示例代码,假设有两个索引 index1 和 index2,需要联合查询: ``` POST /_msearch {} {"index": "index1"} {"query": {"match_all": {}}} {} {"index": "index2"} {"query": {"match_all": {}}} ``` 上述代码中,`_msearch` 是 Multi-Search API,`index1` 和 `index2` 是要查询的两个索引,`match_all` 是一个简单的查询语句,表示匹配所有文档。查询结果会按照查询顺序依次返回,需要自行解析和处理。 需要注意的是,多索引联合查询可能会带来一些性能问题,特别是在大数据量场景下。因此,需要根据实际情况进行权衡和优化。 ### 回答2: Elasticsearch是一个分布式搜索引擎,可以用于存储、搜索和分析大规模的数据集合。在Elasticsearch中,我们可以使用多索引联合查询来实现对多个索引中的数据进行查询和分析。 多索引联合查询Elasticsearch中非常常见和重要。当我们有多个索引,每个索引包含不同类型或字段的数据时,我们可以使用多索引联合查询来同时搜索这些索引,并获取跨多个索引的结果。 使用多索引联合查询的步骤如下: 1. 创建索引:首先,我们需要创建多个索引,并将不同类型或字段的数据分别存储在这些索引中。 2. 查询语句:在进行多索引联合查询之前,我们需要构建一个查询语句。查询语句可以使用Elasticsearch提供的查询DSL(Domain Specific Language)来编写,通过指定不同的索引名称、查询条件和过滤条件来实现。 3. 查询执行:一旦查询语句准备好,我们可以将其发送到Elasticsearch服务器进行查询执行。Elasticsearch会同时搜索多个索引,并返回跨多个索引的结果。 4. 结果处理:最后,我们可以对查询结果进行处理和分析。可以根据需要,对结果进行排序、筛选、聚合等操作。 多索引联合查询在实际的应用场景中非常有用。例如,当我们的数据被分散存储在不同的索引中,需要同时查询和分析这些数据时,我们可以使用多索引联合查询来快速获取所需的结果。 总结而言,Elasticsearch提供了多索引联合查询的功能,可以方便地搜索和分析跨多个索引的数据。通过构建查询语句、执行查询并处理结果,我们可以快速获取我们所需的数据。 ### 回答3: Elasticsearch是一种开源的分布式搜索引擎,它可用于实现全文搜索、日志分析、数据可视化和实时数据分析等功能。在Elasticsearch中,可以通过多索引联合查询来同时搜索多个索引并获取结果。 多索引联合查询可以通过以下几种方式实现: 1. 使用多个索引名称:可以在查询语句中指定多个索引名称,用逗号分隔。例如,可以使用以下语句同时查询index1和index2两个索引: GET index1,index2/_search { "query": { "match": { "field": "value" } } } 2. 使用通配符查询多个索引:可以使用通配符在查询语句中匹配多个索引名称。例如,可以使用以下语句查询所有以"index"开头的索引: GET index*/_search { "query": { "match": { "field": "value" } } } 3. 使用别名查询多个索引:在创建索引时,可以为索引设置一个别名,然后在查询中使用别名来查询多个索引。例如,可以使用以下语句创建两个索引并为它们设置别名: PUT index1/_alias/myalias PUT index2/_alias/myalias 然后,可以使用以下语句查询myalias别名所对应的索引: GET myalias/_search { "query": { "match": { "field": "value" } } } 多索引联合查询可以帮助我们在一个请求中同时搜索多个索引,提高查询效率和性能。在进行多索引联合查询时,需要注意索引之间的数据结构和映射是否一致,以保证查询结果的准确性和一致性。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值