2013-12-22 66 views
2

我们在elasticsearch中有一个域名索引(我们使用ruby连接和维护这个轮胎宝石),但是我们在精确搜索时遇到了麻烦。elasticsearch与破折号完全匹配

如果我在域中搜索术语google.com,它会带回google.com,但它还会带回任何带有破折号( - )的域,例如in-google.com,research会让我相信 - 是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'       } 

我已经尝试了不同的分析仪,你可以在上面看到,但使用“头”插件搜索时,他们都具有相同的问题。

https://gist.github.com/anonymous/8080839是我用来生成数据集来测试的代码,我在寻找的是能够搜索JUST谷歌,如果我想*谷歌我可以实现我自己的通配符?

我辞职的事实,我将不得不删除并重新生成我的索引,但无论我选择什么样的分析或类型,我仍然不能得到一个确切的匹配

回答

2

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

此外,您可能希望将multi_field -approach检查出分析事物的多种方式。

我做了一堆那说明这个不同的查询可运行的例子。请注意,域名已经在两个方面被索引,并注意查询打哪场:https://www.found.no/play/gist/ecc52fad687e83ddcf73

#!/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" 
      } 
     } 
    } 
} 
' 
+0

亚历您好,感谢回答,我有点不确定我理解你的榜样,我设置了如你所建议的multi_field方法(谢谢),但我仍然有问题搜索确切的域,你给的两个例子查询仍然显示in-google.com,即使搜索查询只是谷歌。 –

+0

对不起,我忘记了在输出中丢失的注释。如果你看这个剧本,应该对他们为什么被收录进行评论。最后一个查询只匹配in-google.com。我已经更新了答案,以包含更多澄清的评论。希望这有助于:) –

+0

我开始了解这一点(和播放)多一点,https://www.found.no/play/gist/dd354aad8703837877cf这是我目前的工作正在进行中多一点的数据,作为你可以看到我有精确的匹配运行良好,但现在是通配符搜索,如果我想搜索谷歌*,在谷歌中仍然会像megoogle一样出现。 –