1

我正在尝试匹配基于 URL 字段的查询。当有人在网页上添加新链接时,我在下面有一个我的 InsertLink 方法。现在,如果要添加任何带有前缀“https://”或“http://”的链接,它会自动匹配第一个(并且仅在这种情况下)带有 https:// 或“http://”的项目/" 索引中的前缀。这是因为我的模型使用 Uri 类型设置的方式吗?这是我的模型示例和 InsertLink 方法的调试屏幕截图。

我的模型:

public class SSOLink
{
    public string Name { get; set; }
    public Uri Url { get; set; }
    public string Owner { get; set; }

}

截图示例。

弹性搜索调试

4

1 回答 1

2

您需要使用UAX_URL 标记器来搜索 URL 字段。

您可以使用 UAX_URL 令牌创建自定义分析器,并使用match您现在使用的相同查询来获得预期结果。

索引映射

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "my_tokenizer"
                }
            },
            "tokenizer": {
                "my_tokenizer": {
                    "type": "uax_url_email",
                    "max_token_length": 5
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "url": {
                "type": "text",
                "analyzer": "my_analyzer"
            }
        }
    }
}

看起来在您的情况下,URL 字段正在使用 Elasticsearch 中的文本字段,它使用标准分析器并使用 _analyze API,您可以检查 URL 字段生成的令牌。

使用标准分析仪

POST _analyze/

{
    "text": "https://www.microsoft.com",
    "analyzer" : "standard"
}

代币

{
    "tokens": [
        {
            "token": "https",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "www.microsoft.com",
            "start_offset": 8,
            "end_offset": 25,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

使用 UAX_URL 标记器

{
    "text": "https://www.microsoft.com",
    "tokenizer" : "uax_url_email"
}

并生成令牌

{
    "tokens": [
        {
            "token": "https://www.microsoft.com",
            "start_offset": 0,
            "end_offset": 25,
            "type": "<URL>",
            "position": 0
        }
    ]
}
于 2020-03-23T17:16:03.427 回答