在当今的大数据时代,搜索引擎成为了我们获取信息的重要工具。而ElasticSearch作为一个强大的开源搜索引擎,提供了丰富的搜索功能。下面,我们将介绍ElasticSearch中的一些常用搜索语句,并通过与SQL的对比来帮助大家更好地理解。
SQL 语句:
SELECT * FROM table_name WHERE column_name = 'value';
ElasticSearch 语句:
{
"query": {
"term": {
"column_name": "value"
}
}
}
SQL 语句:
SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2;
ElasticSearch 语句:
{
"query": {
"range": {
"column_name": {
"gte": "value1",
"lte": "value2"
}
}
}
}
SQL 语句:
SELECT * FROM table_name WHERE column_name LIKE '%value%';
ElasticSearch 语句:
{
"query": {
"wildcard": {
"column_name": "*value*"
}
}
}
SQL 语句:
SELECT * FROM table_name WHERE column1 = 'value1' AND column2 = 'value2';
ElasticSearch 语句:
{
"query": {
"bool": {
"must": [
{ "term": { "column1": "value1" } },
{ "term": { "column2": "value2" } }
]
}
}
}
SQL 语句:(注意:不是所有数据库都支持正则表达式查询)
SELECT * FROM table_name WHERE column_name REGEXP 'pattern';
ElasticSearch 语句:(使用regexp查询)
{
"query": { "regexp": { "column_name": "pattern" } }
} 参数: `{ "query": { "regexp": { "column_name": "pattern" } } }` `= { "query": { "wildcard": { "column_name": "*pattern*" } } }` `= { "query": { "regexp": { "column_name": "*pattern*" } } }` `= { "query": { "wildcard": { "column_name": "*pattern*" } } }` `= { "query": { "regexp": { "column_name": "*pattern*" } } }`