2014-01-27 22 views
8

我最近開始使用ElasticSearch。我嘗試完成一些用例。我遇到了其中一個問題。ElasticSearch - 返回查詢的方面的完整值

我已將某些用戶的全名(例如「Jean-Paul Gautier」,「Jean De La Fontaine」)編入索引。

我嘗試獲取所有響應某個查詢的全名。

例如,我想100個最常見的全名beggining由「J」

{ 
    "query": { 
    "query_string" : { "query": "full_name:J*" } } 
    }, 
    "facets":{ 
    "name":{ 
     "terms":{ 
     "field": "full_name", 
     "size":100 
     } 
    } 
    } 
} 

結果我得到的是全名的所有的話:「讓」,「祿」,「戈蒂埃「,」德「,」拉「,」方丹「。

如何獲得「Jean-Paul Gautier」和「Jean De La Fontaine」(所有full_name值由'J'求助)? 「post_filter」選項沒有這樣做,它只是限制了這個子集。

  • 我必須配置「如何運作」這個FULL_NAME方面
  • 我有一些選項添加到這個當前查詢
  • 我不得不做一些「映射」(非常的時刻晦澀)

感謝

回答

13

你只需要設置"index": "not_analyzed"在球場上,你將能夠全額取回,未修改字段值在你的方面。

通常情況下,最好有一個版本的字段不被分析(用於分面),另一個版本是(用於搜索)。 "multi_field"字段類型對此很有用。

因此,在這種情況下,我可以如下定義映射:

curl -XPUT "http://localhost:9200/test_index/" -d' 
{ 
    "mappings": { 
     "people": { 
     "properties": { 
      "full_name": { 
       "type": "multi_field", 
       "fields": { 
        "untouched": { 
        "type": "string", 
        "index": "not_analyzed" 
        }, 
        "full_name": { 
        "type": "string" 
        } 
       } 
      } 
     } 
     } 
    } 
}' 

這裏,我們有兩個子領域。與父項名稱相同的項目將成爲默認項目,因此如果您在"full_name"字段中進行搜索,則Elasticsearch實際上將使用"full_name.full_name""full_name.untouched"會給你想要的方面結果。

所以接下來我添加了兩個文件:

curl -XPUT "http://localhost:9200/test_index/people/1" -d' 
{ 
    "full_name": "Jean-Paul Gautier" 
}' 

curl -XPUT "http://localhost:9200/test_index/people/2" -d' 
{ 
    "full_name": "Jean De La Fontaine" 
}' 

,然後我可以刻面的每個域,查看返回什麼:

curl -XPOST "http://localhost:9200/test_index/_search" -d' 
{ 
    "size": 0, 
    "facets": { 
     "name_terms": { 
     "terms": { 
      "field": "full_name" 
     } 
     }, 
     "name_untouched": { 
     "terms": { 
      "field": "full_name.untouched", 
      "size": 100 
     } 
     } 
    } 
}' 

,我回來了以下內容:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "facets": { 
     "name_terms": { 
     "_type": "terms", 
     "missing": 0, 
     "total": 7, 
     "other": 0, 
     "terms": [ 
      { 
       "term": "jean", 
       "count": 2 
      }, 
      { 
       "term": "paul", 
       "count": 1 
      }, 
      { 
       "term": "la", 
       "count": 1 
      }, 
      { 
       "term": "gautier", 
       "count": 1 
      }, 
      { 
       "term": "fontaine", 
       "count": 1 
      }, 
      { 
       "term": "de", 
       "count": 1 
      } 
     ] 
     }, 
     "name_untouched": { 
     "_type": "terms", 
     "missing": 0, 
     "total": 2, 
     "other": 0, 
     "terms": [ 
      { 
       "term": "Jean-Paul Gautier", 
       "count": 1 
      }, 
      { 
       "term": "Jean De La Fontaine", 
       "count": 1 
      } 
     ] 
     } 
    } 
} 

正如您所看到的,分析的字段返回單個字,較小的標記(當您未指定分析r,則使用standard analyzer),並且未分析的子字段返回未修改的原始文本。

這是一個可運行的例子,你可以玩: http://sense.qbox.io/gist/7abc063e2611846011dd874648fd1b77450b19a5

+0

經過一些困難,以更新我目前的映射,我成功了!感謝您的寶貴幫助。 – pierallard

2

嘗試改變映射 「FULL_NAME」:

"properties": { 
    "full_name": { 
    "type": "string", 
    "index": "not_analyzed" 
    } 
    ... 
} 

not_analyzed意味着它將保持原樣,首都,空格,破折號等,以便「Jean De La Fontaine」保持可找到的狀態,不會被標記爲「Jean」「De」「La」「Fontaine」

您可以experiment with different analyzers using the api

注意什麼標準的人做的多功能使用部分名稱:

GET /_analyze?analyzer=standard 
{'Jean Claude Van Dame'} 


{ 
    "tokens": [ 
     { 
     "token": "jean", 
     "start_offset": 2, 
     "end_offset": 6, 
     "type": "<ALPHANUM>", 
     "position": 1 
     }, 
     { 
     "token": "claude", 
     "start_offset": 7, 
     "end_offset": 13, 
     "type": "<ALPHANUM>", 
     "position": 2 
     }, 
     { 
     "token": "van", 
     "start_offset": 14, 
     "end_offset": 17, 
     "type": "<ALPHANUM>", 
     "position": 3 
     }, 
     { 
     "token": "dame", 
     "start_offset": 18, 
     "end_offset": 22, 
     "type": "<ALPHANUM>", 
     "position": 4 
     } 
    ] 
} 
+0

感謝「分析」的鏈接,這是非常有用的! – pierallard

相關問題