【问题标题】:ElasticSearch query matching incorrectlyElasticSearch 查询匹配不正确
【发布时间】:2020-03-23 17:11:34
【问题描述】:

我正在尝试根据 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; }

}

截图示例。

【问题讨论】:

    标签: java elasticsearch elasticsearch-query resthighlevelclient


    【解决方案1】:

    您需要使用UAX_URL tokenizer 来搜索 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
            }
        ]
    }
    

    【讨论】:

    • 模型类型是Uri还是String有关系,还是没有相关性?
    • 这最终取决于您映射 URI 字段的弹性数据类型
    • @Parakoopa,你能解决它还是有其他问题
    • 我正在处理它,需要删除我的索引以重新创建它。
    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    相关资源
    最近更新 更多