快速入门:在门户中使用“搜索浏览器”来运行查询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 verbose JSON documents.

先决条件Prerequisites

开始之前,必须具备以下先决条件:Before you begin, have the following prerequisites in place:

  • 具有活动订阅的 Azure 帐户。An Azure account with an active subscription. 试用订阅Trial Subscription.

  • 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 Quickstart: Create an index to create the index using default values. Microsoft 托管的内置示例数据源 (realestate-us-sample) 提供了数据。A built-in sample data source hosted by Microsoft (realestate-us-sample) provides the data.

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

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

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

    门户中的“搜索浏览器”命令

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

    “搜索浏览器”选项卡

未指定查询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

不合格查询或空查询示例

自由格式查询(带或不带运算符)可用于模拟从自定义应用发送到 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.

自由文本查询示例

匹配文档计数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. 回想一下,服务默认返回前 50 个匹配项,因此索引中的匹配项可能比结果中包含的匹配项更多。Recall that the service returns the top 50 matches by default, so you might have more matches in the index than what's included in the results.

$count=true

结果Results

索引中的匹配文档计数

限制搜索结果中的字段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

限制搜索结果中的字段

返回下一批结果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. 可以通过查看文档键 (listingID) 来识别文档。You can check the document key (listingID) to identify a document.

前面提到,需要提供搜索条件,例如查询词或表达式,以便获得排列好的结果。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

返回下一批搜索结果

筛选表达式(大于、小于、等于)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

按条件筛选

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

更改排序顺序

“$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. 查询表达式中的 $select 参数可限制返回哪些字段。The $select parameter in a query expression can 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 implemented 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.