ElasticSearch应该使用什么分析器来搜索url片段和精确url路径

时间:2013-10-02 12:37:46

标签: elasticsearch

我想将uri存储在映射中,我想通过以下方式搜索它:

  • 完全匹配(即如果我存储了http://stackoverflow.com/questions,那么查找术语http://stackoverflow.com/questions会检索该项目。

  • 像字母标记器一样,所有“单词”都应该是可搜索的。因此,搜索questionsstackoverflowcom会使http://stackoverflow.com/questions成为热门。

  • 寻找'。'或'/'分隔的url片段应该仍然是可搜索的。因此,搜索stackoverflow.com会将http://stackoverflow.com/questions作为点击。

  • 应该不区分大小写。 (如小写)

  • html://htmls://www.等是可选的搜索。因此,搜索http://stackoverflow.comstackoverflow.com会将http://stackoverflow.com/questions作为点击。

也许解决方案应该像链接标记符或类似的东西。我对ES很陌生,所以这可能是一个微不足道的问题。 那么我应该使用什么样的分析器来构建这个功能呢?

任何帮助都会受到极大的关注。

1 个答案:

答案 0 :(得分:1)

你是绝对正确的。您需要将字段类型设置为multi_field,然后为每个方案创建分析器。然后,您可以执行multi_match查询:

=============type properties===============
{
    "fun_documents": {
        "properties": {
            "url": {
                "type": "multi_field",
                "fields": {
                    "keyword": {
                        "type": "string",
                        "analyzer": "keyword"
                    },
                    "alphanum_only": {
                        "type": "string",
                        "analyzer": "my_custom_alpha_num_analyzer"
                    },
                    {
                        "etc": "etc"
                    }
                }
            }
        }
    }
}

==================query=====================
{
    "query": {
        "multi_match": {
            "query": "stackoverflow",
            "fields": [
                "url.keyword",
                "url.alphanum_only",
                "url.optional_fun"
            ]
        }
    }
}

请注意,您可以使用multi_field别名并重复使用相同的名称,但这只是简单的演示。

相关问题