快速入门:在门户中使用“搜索浏览器”来运行查询Quickstart: Use Search explorer to run queries in the portal

“搜索浏览器”是一种内置查询工具,用于针对 Azure 认知搜索中的搜索索引运行查询。Search explorer is a built-in query tool used for running queries against a search index in Azure Cognitive Search. 利用此工具,可以轻松学习查询语法,测试查询或筛选器表达式,或者通过验证索引中是否存在较新的内容来确认数据刷新。This tool makes it easy to learn query syntax, test a query or filter expression, or confirm data refresh by checking whether new content exists in the index.

本快速入门使用现有索引来演示搜索浏览器。This quickstart uses an existing index to demonstrate Search explorer. 请求是使用搜索 REST API 表述的,响应以 JSON 文档的形式返回。Requests are formulated using the Search REST API, with responses returned as JSON documents.

先决条件Prerequisites

在开始之前,必须满足以下条件:Before you begin, you must have the following:

  • 具有活动订阅的 Azure 帐户。An Azure account with an active subscription. 创建试用帐户Create an account for trial.

  • Azure 认知搜索服务。An Azure Cognitive Search service. 创建服务或在当前订阅下查找现有服务Create a service or find an existing service under your current subscription. 可以使用本快速入门的免费服务。You can use a free service for this quickstart.

  • 本快速入门使用了 realestate-us-sample-index。The realestate-us-sample-index is used for this quickstart. 使用导入数据向导创建索引。Use the Import data wizard to create the index. 在第一步中,当系统要求输入数据源时,请选择“示例”,然后选择“realestate-us-sample”数据源。In the first step, when asked for the data source, choose Samples and then select the realestate-us-sample data source. 接受所有向导默认设置以创建索引。Accept all of the wizard defaults to create the index.

启动搜索资源管理器Start Search explorer

  1. Azure 门户中,从仪表板打开搜索服务页,或者查找你的服务In the Azure portal, open the search service page from the dashboard or find your service.

  2. 从命令栏打开“搜索浏览器”:Open Search explorer from the command bar:

    门户中的“搜索浏览器”命令Search explorer command in portal

    或在打开的索引上使用嵌入的“搜索浏览器”选项卡:Or use the embedded Search explorer tab on an open index:

    “搜索浏览器”选项卡Search explorer tab

未指定查询Unspecified query

若要首先查看内容,请执行空搜索,方法是单击“搜索”,不提供搜索词。For a first look at content, execute an empty search by clicking Search with no terms provided. 空搜索作为第一个查询十分有用,因为它返回全部文档,以便查看文档组合。An empty search is useful as a first query because it returns entire documents so that you can review document composition. 空搜索没有搜索级别,按任意顺序返回文档(所有文档都为 "@search.score": 1)。On an empty search, there is no search rank and documents are returned in arbitrary order ("@search.score": 1 for all documents). 默认情况下,搜索请求中会返回 50 个文档。By default, 50 documents are returned in a search request.

空搜索的等效语法是 *search=*Equivalent syntax for an empty search is * or search=*.

search=*

结果Results

空查询示例Empty query example

自由格式查询(带或不带运算符)可用于模拟从自定义应用发送到 Azure 认知搜索的用户定义查询。Free-form queries, with or without operators, are useful for simulating user-defined queries sent from a custom app to Azure Cognitive Search. 只会扫描在索引定义中将其属性设置为“可搜索”的那些字段来查找匹配项。Only those fields attributed as Searchable in the index definition are scanned for matches.

请注意,如果提供了搜索条件(例如查询词或表达式),则会应用搜索排名。Notice that when you provide search criteria, such as query terms or expressions, search rank comes into play. 以下示例对自定义文本搜索进行了说明。The following example illustrates a free text search.

Seattle apartment "Lake Washington" miele OR thermador appliance

结果Results

可以使用 Ctrl-F 在结果中搜索感兴趣的特定字词。You can use Ctrl-F to search within results for specific terms of interest.

自由文本查询示例Free text query example

匹配文档计数Count of matching documents

添加 $count=true 以获取在索引中找到的匹配项数。Add $count=true to get the number of matches found in an index. 在空搜索中,计数是指索引中的文档总数。On an empty search, count is the total number of documents in the index. 在限定搜索中,计数是与查询输入匹配的文档数。On a qualified search, it's the number of documents matching the query input.

$count=true

结果Results

文档计数示例Count of documents example

限制搜索结果中的字段Limit fields in search results

添加 $select,将结果限制为显式命名的字段,以便在“搜索浏览器”中获得可读性更强的输出。Add $select to limit results to the explicitly named fields for more readable output in Search explorer. 若要保留搜索字符串“$count=true”,请在参数前面加上 & 。To keep the search string and $count=true, prefix arguments with &.

search=seattle condo&$select=listingId,beds,baths,description,street,city,price&$count=true

结果Results

限制字段示例Limit fields example

返回下一批结果Return next batch of results

Azure 认知搜索根据搜索排名返回排名前 50 的匹配项。Azure Cognitive Search returns the top 50 matches based on the search rank. 若要获取下一组匹配的文档,请附加“$top=100,&$skip=50”,这会将结果集增加为 100 个文档(默认值为 50,最大值为 1000)并跳过前 50 个文档。To get the next set of matching documents, append $top=100,&$skip=50 to increase the result set to 100 documents (default is 50, maximum is 1000), skipping the first 50 documents. 前面提到,需要提供搜索条件,例如查询词或表达式,以便获得排列好的结果。Recall that you need to provide search criteria, such as a query term or expression, to get ranked results. 请注意,搜索分数随搜索结果中搜索的深入而降低。Notice that search scores decrease the deeper you reach into search results.

search=seattle condo&$select=listingId,beds,baths,description,street,city,price&$count=true&$top=100&$skip=50

结果Results

批量搜索结果Batch search results

筛选表达式(大于、小于、等于)Filter expressions (greater than, less than, equal to)

如果要指定精确条件搜索,而不是进行自定义文本搜索,请使用 $filter 参数。Use the $filter parameter when you want to specify precise criteria rather than free text search. 必须在索引中将此字段的属性设置为“可筛选”。The field must be attributed as Filterable in the index. 此示例搜索大于 3 间的卧室:This example searches for bedrooms greater than 3:

search=seattle condo&$filter=beds gt 3&$count=true

结果Results

筛选器表达式Filter expression

Order-by 表达式Order-by expressions

添加 $orderby,按搜索分数之外的其他字段对结果排序。Add $orderby to sort results by another field besides search score. 必须在索引中将此字段的属性设置为“可排序”。The field must be attributed as Sortable in the index. 可用于测试此功能的示例表达式如下所示:An example expression you can use to test this out is:

search=seattle condo&$select=listingId,beds,price&$filter=beds gt 3&$count=true&$orderby=price asc

结果Results

Orderby 表达式Orderby expression

“$filter”和“$orderby”表达式都是 OData 构造 。Both $filter and $orderby expressions are OData constructions. 有关详细信息,请参阅 Filter OData syntax(筛选器 OData 语法)。For more information, see Filter OData syntax.

要点Takeaways

在本快速入门中,你使用“搜索浏览器”通过 REST API 查询了一个索引。In this quickstart, you used Search explorer to query an index using the REST API.

  • 结果会作为详细的 JSON 文档返回,以便可以完整地查看文档的构建情况和内容。Results are returned as verbose JSON documents so that you can view document construction and content, in entirety. 可以使用示例中显示的查询表达式来限制返回的字段。You can use query expressions, shown in the examples, to limit which fields are returned.

  • 文档由标记为在索引中“可检索”的所有字段构成。Documents are composed of all fields marked as Retrievable in the index. 若要在门户中查看索引属性,请单击搜索概述页面上“索引”列表中的“realestate-us-sample”。To view index attributes in the portal, click realestate-us-sample in the Indexes list on the search overview page.

  • 自由格式查询类似于在商用 Web 浏览器中输入的内容,可用于测试最终用户体验。Free-form queries, similar to what you might enter in a commercial web browser, are useful for testing an end-user experience. 例如,假设有一个内置的房地产示例索引,可以输入“华盛顿湖西雅图公寓”,再使用 Ctrl-F 在搜索结果中查找字词。For example, assuming the built-in realestate sample index, you could enter "Seattle apartments lake washington", and then you can use Ctrl-F to find terms within the search results.

  • 查询和筛选器表达式使用 Azure 认知搜索支持的语法来表达。Query and filter expressions are articulated in a syntax supported by Azure Cognitive Search. 默认为简单语法,但可选择使用完整 Lucene 进行更强大的查询。The default is a simple syntax, but you can optionally use full Lucene for more powerful queries. 筛选表达式采用的是 OData 语法。Filter expressions are an OData syntax.

清理资源Clean up resources

在自己的订阅中操作时,最好在项目结束时确定是否仍需要已创建的资源。When you're working in your own subscription, it's a good idea at the end of a project to identify whether you still need the resources you created. 持续运行资源可能会产生费用。Resources left running can cost you money. 可以逐个删除资源,也可以删除资源组以删除整个资源集。You can delete resources individually or delete the resource group to delete the entire set of resources.

可以使用左侧导航窗格中的“所有资源”或“资源组”链接 ,在门户中查找和管理资源。You can find and manage resources in the portal, using the All resources or Resource groups link in the left-navigation pane.

如果使用的是免费服务,请记住只能设置三个索引、索引器和数据源。If you are using a free service, remember that you are limited to three indexes, indexers, and data sources. 可以在门户中删除单个项目,以不超出此限制。You can delete individual items in the portal to stay under the limit.

后续步骤Next steps

若要详细了解查询结构和语法,请使用 Postman 或某个等效工具创建可利用该 API 的更多部分的查询表达式。To learn more about query structures and syntax, use Postman or an equivalent tool to create query expressions that leverage more parts of the API. 搜索 REST API 对于学习和探索特别有用。The Search REST API is especially helpful for learning and exploration.