2012-03-14 29 views
1

時奇怪的搜索行爲所以我們可以說我有一個這樣定義的ElasticSearch指數:ElasticSearch:使用雪球分析儀

curl -XPUT 'http://localhost:9200/test' -d '{ 
    "mappings": { 
    "example": { 
     "properties": { 
     "text": { 
      "type": "string", 
      "analyzer": "snowball" 
     } 
     } 
    } 
    } 
}' 

curl -XPUT 'http://localhost:9200/test/example/1' -d '{ 
    "text": "foo bar organization" 
}' 

當我搜索「富企業」打雪仗分析,這兩個關鍵字匹配預期:

curl -XGET http://localhost:9200/test/example/_search -d '{ 
    "query": { 
    "text": { 
     "_all": { 
     "query": "foo organizations", 
     "analyzer": "snowball" 
     } 
    } 
    }, 
    "highlight": { 
    "fields": { 
     "text": {} 
    } 
    } 
}' 

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 0.015912745, 
    "hits": [ 
     { 
     "_index": "test", 
     "_type": "example", 
     "_id": "1", 
     "_score": 0.015912745, 
     "_source": { 
      "text": "foo bar organization" 
     }, 
     "highlight": { 
      "text": [ 
      "<em>foo</em> bar <em>organization</em>" 
      ] 
     } 
     } 
    ] 
    } 
} 

但是,當我搜索只對「組織」我不明白,在所有的任何結果,這是非常奇怪:

curl -XGET http://localhost:9200/test/example/_search -d '{ 
    "query": { 
    "text": { 
     "_all": { 
     "query": "organizations", 
     "analyzer": "snowball" 
     } 
    } 
    }, 
    "highlight": { 
    "fields": { 
     "text": {} 
    } 
    } 
}' 

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 0, 
    "max_score": null, 
    "hits": [] 
    } 
} 

不過,如果我搜索「酒吧」它仍然點擊:

curl -XGET http://localhost:9200/test/example/_search -d '{ 
    "query": { 
    "text": { 
     "_all": { 
     "query": "bars", 
     "analyzer": "snowball" 
     } 
    } 
    }, 
    "highlight": { 
    "fields": { 
     "text": {} 
    } 
    } 
}' 

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 0.10848885, 
    "hits": [ 
     { 
     "_index": "test", 
     "_type": "example", 
     "_id": "1", 
     "_score": 0.10848885, 
     "_source": { 
      "text": "foo bar organization" 
     }, 
     "highlight": { 
      "text": [ 
      "foo <em>bar</em> organization" 
      ] 
     } 
     } 
    ] 
    } 
} 

我猜「吧」和「組織」之間的區別是,「組織」是朵朵給「器官」,而「巴」是源於自己。但是,如何獲得正確的行爲以便第二次搜索?

回答

1

文本「富酒吧組織」是獲得索引兩次 - 在該領域文本並在現場_all。該字段文字正在使用雪球分析儀,而字段_all正在使用標準分析儀。因此,在分析測試記錄後,字段_all包含標記:「foo」,「bar」和「organization」。在搜索期間,指定的雪球分析儀將「foo」轉換爲「foo」,「bars」轉換爲「bar」和「organization」轉換爲「organ」。因此,查詢中的單詞「foo」和「bars」與測試記錄相匹配,而術語「組織」不包含。突出顯示是在每個字段的基礎上執行的,與搜索無關,這就是爲什麼在第一個結果中突出顯示單詞「組織」的原因。

+0

這很有道理,謝謝。有沒有一種方法可以查看我的索引具有的實際令牌?我能告訴ES爲** _ all **列使用雪球嗎? – tycooon 2012-03-15 07:01:38

+0

沒有簡單的方法來查看索引中的實際令牌,但可以使用Analyze API查看如何分析文本(http://www.elasticsearch.org/guide/reference/api/admin-indices- analyze.html)。您可以在創建索引期間爲* _all *字段設置雪球分析器https://gist.github.com/2043721。或者,可以將雪球設置爲所有字段的默認分析器https://gist.github.com/2043697。 – imotov 2012-03-15 11:23:45

0

在索引時間使用分析儀比搜索時間更好..將您的文本字段映射到雪球分析儀然後索引。這將爲組織創建一些代幣,包括組織。它適用於我