【问题标题】:Azure Search - querying for ampersand characterAzure 搜索 - 查询 & 字符
【发布时间】:2020-05-08 03:29:39
【问题描述】:

我正在尝试查找所有包含& 符号的记录,该符号是保留的。我使用的是search,而不是$filter

根据文档,它不能用\% 转义,应该作为HTML url 部分转义到%26

尝试使用 SDK 和搜索资源管理器查找有关如何搜索的任何选项,但没有成功:

  1. &
  2. *&*
  3. *%26*
  4. %26
  5. \%26

UPD

文档示例:

{
    "test": "Hello & World"

搜索查询:search=%26&searchFields=test&$select=test

UPD 2

public class MyType
{
    [System.ComponentModel.DataAnnotations.Key]
    [IsFilterable]
    public string Id { get; set; }

    [IsSearchable, IsSortable]
    public string Test { get; set; }
}

class Program
    {
        private static SearchServiceClient CreateSearchServiceClient()
        {
            string searchServiceName = "XXXXXX";
            string adminApiKey = "XXXXXXX";

            var serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
            return serviceClient;
        }

        static void Main(string[] args)
        {
            var client = CreateSearchServiceClient();
            var def = new Microsoft.Azure.Search.Models.Index
            {
                Name = "temp-test-reserved1",
                Fields = FieldBuilder.BuildForType<MyType>()
            };
            client.Indexes.Create(def);
            var c = client.Indexes.GetClient("temp-test-reserved1");

            var actions = new IndexAction<MyType>[]
            {
                IndexAction.Upload(new MyType{ Id = "1", Test = "Hello & World" }),
                IndexAction.Upload(new MyType{ Id = "2", Test = "& test start" }),
                IndexAction.Upload(new MyType{ Id = "3", Test = "test end &" })
            };
            c.Documents.Index(IndexBatch.New(actions));
        }
    }

search=%26&amp;searchFields=Test&amp;$select=Test

【问题讨论】:

  • 您是否在 SDK 和搜索资源管理器中尝试了所有这些选项?我在下面发布了一个答案,但对于 SDK 选项 1 应该刚刚工作,除非您更改默认值。如果不起作用,您可以发布一个代码示例吗
  • 另外,只是为了确保 - 它应该转义到 %26(如您的列表中所述)而不是 %24(如上面的句子中所述)
  • 我想用搜索资源管理器修复它。更新帖子
  • 还有其他原因导致文档被过滤掉吗?您能否尝试使用关键字分析器(而不是默认分析器)docs.microsoft.com/en-us/azure/search/… 看看是否有帮助?
  • 这种方法没有帮助。实际上,在我们的案例中,我不认为将整个字段作为标记是可以的。我们需要在该字段上进行全文评分搜索。此外,我们正在使用搜索和过滤功能

标签: azure-cognitive-search azure-search-.net-sdk


【解决方案1】:

您可能找不到&amp;,因为它在索引和查询时被删除,因为它被默认分析器视为标点符号。 This article 提供有关 Azure 认知搜索中词法分析的更多详细信息。如果您需要保留 & 符号,同时仍对空白边界上的文本进行标记,则需要创建一个 custom analyzer

【讨论】:

    【解决方案2】:

    在 Azure 门户的搜索资源管理器字段中尝试查询 &amp;search=%26(如下所示)

    在 SDK 中,只有将 UseHttpGetForQueries 参数设置为 true,您才需要考虑对 '&' 字符进行 URL 编码。默认情况下,此参数设置为false,在这种情况下您不需要对其进行编码。

    更多关于转义/编码here的文档

    【讨论】:

    • 搜索资源管理器的示例不起作用。它什么也不返回
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多