elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire" /> elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire" />

Ruby on rails elasticsearch与破折号精确匹配

Ruby on rails elasticsearch与破折号精确匹配,ruby-on-rails,ruby,elasticsearch,tire,Ruby On Rails,Ruby,elasticsearch,Tire,我们在elasticsearch中有一个域名索引(我们正在使用tire gem和ruby来连接和维护它),但是我们在精确搜索方面遇到了麻烦 如果我在域中搜索google.com这个词,它会返回google.com,但是它也会返回任何带有破折号(-)的域,比如in-google.com,这让我相信-在ES中是一个通配符,我需要做的就是不进行分析,但这不起作用 :domain => { :type => 'string' , :analyzer => 'whit

我们在elasticsearch中有一个域名索引(我们正在使用tire gem和ruby来连接和维护它),但是我们在精确搜索方面遇到了麻烦

如果我在域中搜索google.com这个词,它会返回google.com,但是它也会返回任何带有破折号(-)的域,比如in-google.com,这让我相信-在ES中是一个通配符,我需要做的就是不进行分析,但这不起作用

    :domain       => { :type => 'string' , :analyzer => 'whitespace'                          },
    :domain_2     => { :type => 'string' , :analyzer => 'pattern'                          },
    :domain_3     => { :type => 'string', :index => 'not_analyzed'                           },
    :domain_4     => { :type => 'string', :analyzer => 'snowball'                            }
正如你在上面看到的,我尝试了不同的分析器,但是当使用“head”插件搜索时,它们都有相同的问题

是我用来生成要测试的数据集的代码,我想要的是只搜索google的能力,如果我想搜索google,我可以实现我自己的通配符


我不得不接受这样一个事实:我将不得不删除并重新生成我的索引,但无论我选择或键入什么分析器,我仍然无法获得精确匹配

您没有显示正在使用的示例查询。您确定您的查询和索引使用相同的文本处理吗

此外,您可能希望查看以多种方式分析事物的方法

我用一系列不同的查询制作了一个可运行的示例来说明这一点。请注意,域已通过两种方式编制索引,并注意查询所涉及的字段:


嗨,Alex,谢谢你的回答,我有点不确定我是否理解你的示例,我已经按照你的建议使用多字段方法设置了索引(谢谢),但是我仍然在搜索确切的域时遇到问题,你给出的两个示例查询仍然显示在-google.com中,即使搜索查询只是google。对不起,我忘了这些评论在导出时丢失了。如果你看这出戏,应该会有关于它们为什么被包括在内的评论。最后一个查询仅与in-google.com匹配。我更新了答案,加入了更清晰的评论。希望这有帮助:)我开始更多地理解这一点(并开始玩),这是我目前正在进行的工作,有更多的数据,正如你所看到的,我已经得到了精确的匹配,但是现在是通配符搜索,如果我想搜索谷歌*,在谷歌中仍然像megoogle一样出现。我已经将此标记为答案,它不完全在那里,但它让我走上了正轨。我不完全确定在游戏中的所有搜索示例中您想要什么,但您不能在
匹配中使用通配符。在某些情况下,您需要
前缀
-查询。(请注意,前缀不进行分析)。仪表板过滤器有点奇怪。有一个可以处理CIDR掩码的
ip_范围
-过滤器,但与您尝试的模式不同。您需要以多种方式对IP进行索引,才能执行所需的查询。
#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
    "mappings": {
        "type": {
            "properties": {
                "domain": {
                    "type": "multi_field",
                    "fields": {
                        "domain": {
                            "type": "string",
                            "analyzer": "standard"
                        },
                        "whitespace": {
                            "type": "string",
                            "analyzer": "whitespace"
                        }
                    }
                }
            }
        }
    }
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"domain":"google.com"}
{"index":{"_index":"play","_type":"type"}}
{"domain":"in-google.com"}
'

# Do searches

# Matches both
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "_all": "google.com"
        }
    }
}
'

# Also matches "google.com". in-google.com gets tokenized to ["in", "google.com"]
# and the default match operator is `or`.
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "domain": {
                "query": "in-google.com"
            }
        }
    }
}
'

# What terms are generated? (Answer: `google.com` and `in`)
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "size": 0,
    "facets": {
        "domain": {
            "terms": {
                "field": "domain"
            }
        }
    }
}
'

# This should just match the second document.
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "match": {
            "domain.whitespace": {
                "query": "in-google.com"
            }
        }
    }
}
'