推荐大良网站建设,目录浏览的网站,网站建设哪家好首推万维科技,织梦配置手机网站query_string查询介绍 1 概述2 基本概念3 数据准备4 query_string查询示例4.1 基本查询4.2 复杂查询解析4.3 高级过滤解析4.4 模糊查询解析4.5 高亮查询解析4.6 分页查询解析 5 总结 大家好#xff0c;我是欧阳方超#xff0c;可以我的公众号“欧阳方超”#xff0c;后续内容… query_string查询介绍 1 概述2 基本概念3 数据准备4 query_string查询示例4.1 基本查询4.2 复杂查询解析4.3 高级过滤解析4.4 模糊查询解析4.5 高亮查询解析4.6 分页查询解析 5 总结 大家好我是欧阳方超可以我的公众号“欧阳方超”后续内容将在公众号首发。
1 概述
Elasticsearch中的query_string查询是一种强大的工具允许用户使用复杂的查询语法来搜索文档。它支持多个字段、布尔逻辑、通配符等功能适合于需要灵活搜索的场景。本文将结合示例详细讲解query_string的用法。
2 基本概念
query_stirng查询使用一种严格的语法来解析用户输入的查询字符串。允许用户使用简洁的字符串实现复杂的查询逻辑它可以分割查询字符串并根据操作符如and、or、not分析每个部分从而返回匹配的文档。
3 数据准备
创建一个存储博客信息的索引并插入一些数据以便后续的查询。
{settings: {number_of_shards: 1,number_of_replicas: 1},mappings: {properties: {title: {type: text},content: {type: text},tags: {type: keyword},author: {type: keyword},publish_date: {type: date,format: yyyy-MM-dd},views: {type: long},status: {type: keyword}}}
}插入数据准备
{index:{_id:1}}
{title:Getting Started with Elasticsearch,content:Elasticsearch is a powerful search and analytics engine. It provides a distributed, multitenant-capable full-text search engine.,tags:[elasticsearch,guide,search],author:John Doe,publish_date:2023-01-15,views:1000,status:published}
{index:{_id:2}}
{title:Advanced Elasticsearch Query Guide,content:Learn about complex queries in Elasticsearch including query_string, bool queries and aggregations.,tags:[elasticsearch,advanced,query],author:Jane Smith,publish_date:2023-02-20,views:800,status:published}
{index:{_id:3}}
{title:Elasticsearch vs Solr Comparison,content:A detailed comparison between Elasticsearch and Solr. Both are powerful search engines built on Apache Lucene.,tags:[elasticsearch,solr,comparison],author:John Doe,publish_date:2023-03-10,views:1200,status:published}
{index:{_id:4}}
{title:Mastering Kibana Dashboards,content:Create powerful visualizations and dashboards using Kibana with Elasticsearch data.,tags:[kibana,elasticsearch,visualization],author:Alice Johnson,publish_date:2023-04-05,views:600,status:draft}
{index:{_id:5}}
{title:Elasticsearch Security Best Practices,content:Learn about securing your Elasticsearch cluster, including authentication, authorization, and encryption.,tags:[elasticsearch,security,best practices],author:Bob Wilson,publish_date:2023-05-01,views:1500,status:published}4 query_string查询示例
4.1 基本查询
简单查询 下面的查询将查询content字段包含powerful字符串的文档并将其返回。
{query: {query_string: {default_field:content,query:powerful}}
}多字段查询 下面的多字段查询的查询逻辑为
在title和content字段中搜索同时包含elasticsearch和security的文档注意只要在两个字段中能匹配到elasticsearch和security即可不要求在这两个字段的每个字段中都能匹配到elasticsearch和security。and操作符要求两个条件都满足
{query: {query_string: {fields:[title,content],query:elasticsearch AND security}}
}只有id5的文档能被查出来因为它的title包含security且content包含elasticsearch。
4.2 复杂查询解析
组合条件查询
{query: {query_string: {fields:[title,content],query:(elasticsearch OR solr) AND (guide OR comparison)}}
}上面的DSL逻辑为
在title和content字段中搜索文档必须满足 包含elasticsearch或solr中的至少一个AND 包含guide或comparison中的至少一个
会查询出两个文档
id2 的文档包含elasticsearch和guideid3 的文档包含elasticsearch/solr和comparison 范围查询
{query: {query_string: {query:elasticsearch AND publish_date:[2023-01-01 TO 2023-03-31] AND views:1000}}
}上面DSL查询逻辑为 搜索满足以下所有条件的文档
包含elasticsearch发布日期在2023-01-01到2023-03-31之间浏览量大于1000 只有id3的文档可以被查询到。
4.3 高级过滤解析
{query: {query_string: {query: status:published AND author:\John Doe\ AND (title:elasticsearch OR content:elasticsearch)}}
}搜索满足以下所有条件的文档
状态为published作者为John Doe标题或内容中包含elasticsearch 最终文档1和3符合条件被查询到。 以下是查询结果
{took: 8,timed_out: false,_shards: {total: 1,successful: 1,skipped: 0,failed: 0},hits: {total: {value: 2,relation: eq},max_score: 1.525382,hits: [{_index: blog_index,_id: 3,_score: 1.525382,_source: {title: Elasticsearch vs Solr Comparison,content: A detailed comparison between Elasticsearch and Solr. Both are powerful search engines built on Apache Lucene.,tags: [elasticsearch,solr,comparison],author: John Doe,publish_date: 2023-03-10,views: 1200,status: published}},{_index: blog_index,_id: 1,_score: 1.5210661,_source: {title: Getting Started with Elasticsearch,content: Elasticsearch is a powerful search and analytics engine. It provides a distributed, multitenant-capable full-text search engine.,tags: [elasticsearch,guide,search],author: John Doe,publish_date: 2023-01-15,views: 1000,status: published}}]}
}4.4 模糊查询解析
{query: {query_string: {query: elasticsearch AND status:published}},size : 0,aggs: {authors: {terms: {field: author}},avg_views: {avg: {field: views}}}
}这是一个用于搜索和聚合数据的请求稍微复杂一些下面详细介绍下。 查询部分
query这是整个查询的主体指定了要执行的搜索操作。query_string这部分使用了查询字符串语法允许通过简单的文本表达式来构建复杂的查询。 query这是的值是elasticsearch AND status:published意味着要搜索包含elasticsearch这个词并且其status字段为published的文档AND确保两个条件都满足。 聚合部分 aggs这个部分用于定义聚合操作可以对查询结果进行统计和分析。 作者聚合 authors这是一个自定义的聚合名称用于统计不同作者的文档数量。 terms指定使用分组聚合terms是桶聚合的一种其作用类似于SQL的group by根据字段分组相同字段值的文档分为一组。 “field”author表示按照author字段的值进行分组结果将返回每个作者及其对应的文档计数。 平均浏览量聚合 avg_views这是另一个自定义聚合名称用于计算文档的平均浏览量。 avg指定平均值聚合。 “field”: “views表示计算views字段的平均值。这将返回所有匹配文档中views字段的平均值。 注意上面的DSL中设置了这将仅返回聚合查询结果不返回普通query查询结果即hits”: []。以下是查询结果
{took: 4,timed_out: false,_shards: {total: 1,successful: 1,skipped: 0,failed: 0},hits: {total: {value: 4,relation: eq},max_score: null,hits: []},aggregations: {avg_views: {value: 1125},authors: {doc_count_error_upper_bound: 0,sum_other_doc_count: 0,buckets: [{key: John Doe,doc_count: 2},{key: Bob Wilson,doc_count: 1},{key: Jane Smith,doc_count: 1}]}}
}4.5 高亮查询解析
{query: {query_string: {query: elasticsearch security}},highlight: {fields: {title: {},content: {}}}
}上面的DSL分查询和高亮两部分下面详细解释一下。
查询部分 query这是整个查询的主体定义了要执行的搜索操作。query_string这个部分使用了查询字符串语法运行通过简单的文本表达式构建复杂的查询。 query这里的值是elasticsearch security这意味着要查找包含elasticsearch和security这两个词的文档。默认情况下elasticsearch将这些词视为单独的词进行处理并使用OR逻辑运算符连接它们这意味着只要文档中包含其中一个词就会被匹配。fields这个参数指定了要搜索的字段这个例子中搜索将在title和content字段中进行只有这两个字段中的内容会被考虑用于匹配查询。 高亮部分 highlight这部分用于定义特殊标记的设置每个文档中匹配的词会被特殊标记默认用标签包围以便在搜索结果中突出显示匹配的内容。 fields指定需要高亮显示的字段上例中指定了title和content字段这意味着当搜索结果返回时如果这些字段中的内容与查询匹配它们将被高亮显示以便用户能够快速识别相关信息。 下面是查询结果
{took: 5,timed_out: false,_shards: {total: 1,successful: 1,skipped: 0,failed: 0},hits: {total: {value: 5,relation: eq},max_score: 1.6386936,hits: [{_index: blog_index,_id: 5,_score: 1.6386936,_source: {title: Elasticsearch Security Best Practices,content: Learn about securing your Elasticsearch cluster, including authentication, authorization, and encryption.,tags: [elasticsearch,security,best practices],author: Bob Wilson,publish_date: 2023-05-01,views: 1500,status: published},highlight: {title: [emElasticsearch/em emSecurity/em Best Practices],content: [Learn about securing your emElasticsearch/em cluster, including authentication, authorization, and encryption]}},{_index: blog_index,_id: 1,_score: 0.28161854,_source: {title: Getting Started with Elasticsearch,content: Elasticsearch is a powerful search and analytics engine. It provides a distributed, multitenant-capable full-text search engine.,tags: [elasticsearch,guide,search],author: John Doe,publish_date: 2023-01-15,views: 1000,status: published},highlight: {title: [Getting Started with emElasticsearch/em],content: [emElasticsearch/em is a powerful search and analytics engine.]}},{_index: blog_index,_id: 2,_score: 0.28161854,_source: {title: Advanced Elasticsearch Query Guide,content: Learn about complex queries in Elasticsearch including query_string, bool queries and aggregations.,tags: [elasticsearch,advanced,query],author: Jane Smith,publish_date: 2023-02-20,views: 800,status: published},highlight: {title: [Advanced emElasticsearch/em Query Guide],content: [Learn about complex queries in emElasticsearch/em including query_string, bool queries and aggregations.]}},{_index: blog_index,_id: 3,_score: 0.28161854,_source: {title: Elasticsearch vs Solr Comparison,content: A detailed comparison between Elasticsearch and Solr. Both are powerful search engines built on Apache Lucene.,tags: [elasticsearch,solr,comparison],author: John Doe,publish_date: 2023-03-10,views: 1200,status: published},highlight: {title: [emElasticsearch/em vs Solr Comparison],content: [A detailed comparison between emElasticsearch/em and Solr.]}},{_index: blog_index,_id: 4,_score: 0.09708915,_source: {title: Mastering Kibana Dashboards,content: Create powerful visualizations and dashboards using Kibana with Elasticsearch data.,tags: [kibana,elasticsearch,visualization],author: Alice Johnson,publish_date: 2023-04-05,views: 600,status: draft},highlight: {content: [Create powerful visualizations and dashboards using Kibana with emElasticsearch/em data.]}}]}
}4.6 分页查询解析
下面是一个使用查询字符串语法进行分页查询的示例
{query: {query_string: {query: elasticsearch security}},from:0,size:4,sort:[{views:desc}]
}有三部分组成查询部分、分页控制部分和排序部分。
查询部分字符串查询语法。分页控制部分 “from”: 0这个参数指定从结果集中的第0个文档开始返回即从第一条记录开始。用于实现分页功能。“size”: 2指定要返回的文档数量。在这个例子中最多返回2条匹配的文档。这与from参数结合使用你可以实现更灵活的分页。 排序部分 sort用于定义如何对搜索结果进行排序。{ “views”: “desc” }表示根据views字段进行降序排序。 下面是返回值
{took: 6,timed_out: false,_shards: {total: 1,successful: 1,skipped: 0,failed: 0},hits: {total: {value: 5,relation: eq},max_score: null,hits: [{_index: blog_index,_id: 5,_score: null,_source: {title: Elasticsearch Security Best Practices,content: Learn about securing your Elasticsearch cluster, including authentication, authorization, and encryption.,tags: [elasticsearch,security,best practices],author: Bob Wilson,publish_date: 2023-05-01,views: 1500,status: published},sort: [1500]},{_index: blog_index,_id: 3,_score: null,_source: {title: Elasticsearch vs Solr Comparison,content: A detailed comparison between Elasticsearch and Solr. Both are powerful search engines built on Apache Lucene.,tags: [elasticsearch,solr,comparison],author: John Doe,publish_date: 2023-03-10,views: 1200,status: published},sort: [1200]}]}
}5 总结
介绍了查询字符串query_string语法并结合一些高级查询展示了查询字符串语法的使用。如果你觉得“查询字符串”这种叫法有些奇怪大可不必因为这完全是安装query_string译过来的。 我是欧阳方超把事情做好了自然就有兴趣了如果你喜欢我的文章欢迎点赞、转发、评论加关注。我们下次见。