ElasticSearch查询匹配不正确

时间:2020-03-23 17:11:34

标签: java elasticsearch elasticsearch-query resthighlevelclient

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

}

截图示例。

ElasticSearch Debug

1 个答案:

答案 0 :(得分:2)

您需要使用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
        }
    ]
}