2015-10-05 59 views
0

我剛剛創造了一些非常簡單的「電影」使用此數據庫教程(指數):http://joelabrahamsson.com/elasticsearch-101/如何使用ElasticSearch正確過濾/排序?

現在,我嘗試複製/粘貼指令打造一個多字段映射爲「導演」字段:

curl -XPUT "http://localhost:9200/movies/movie/_mapping" -d' 
{ 
    "movie": { 
     "properties": { 
     "director": { 
      "type": "multi_field", 
      "fields": { 
       "director": {"type": "string"}, 
       "original": {"type" : "string", "index" : "not_analyzed"} 
      } 
     } 
     } 
    } 
}' 

但在此之後,如果我張貼此查詢,我沒有得到任何結果:

curl -XPOST "http://localhost:9200/_search" -d' 
{ 
    "query": { 
     "constant_score": { 
      "filter": { 
       "term": { "director.original": "Francis Ford Coppola" } 
      } 
     } 
    } 
}' 

結果:

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

如果我嘗試使用排序是:

http://localhost:9200/movies/movie/_search?sort=title.original:asc 

我得到隨機順序整個表(類型)(順序相同,沒有「排序」命令):

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":6,"max_score":null,"hits":[{"_index":"movies","_type":"movie","_id":"4","_score":null,"_source": 
{ 
    "title": "Apocalypse Now", 
    "director": "Francis Ford Coppola", 
    "year": 1979, 
    "genres": ["Drama", "War"] 
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"5","_score":null,"_source": 
{ 
    "title": "Kill Bill: Vol. 1", 
    "director": "Quentin Tarantino", 
    "year": 2003, 
    "genres": ["Action", "Crime", "Thriller"] 
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"1","_score":null,"_source": 
{ 
    "title": "The Godfather", 
    "director": "Francis Ford Coppola", 
    "year": 1972, 
    "genres": ["Crime", "Drama"] 
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"6","_score":null,"_source": 
{ 
    "title": "The Assassination of Jesse James by the Coward Robert Ford", 
    "director": "Andrew Dominik", 
    "year": 2007, 
    "genres": ["Biography", "Crime", "Drama"] 
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"2","_score":null,"_source": 
{ 
    "title": "Lawrence of Arabia", 
    "director": "David Lean", 
    "year": 1962, 
    "genres": ["Adventure", "Biography", "Drama"] 
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"3","_score":null,"_source": 
{ 
    "title": "To Kill a Mockingbird", 
    "director": "Robert Mulligan", 
    "year": 1962, 
    "genres": ["Crime", "Drama", "Mystery"] 
},"sort":[null]}]}} 

那麼你會告訴我在ElasticSearch的基本使用中我缺少什麼?爲什麼不對我的自定義「導演」字段進行過濾或排序?

+0

注意你的'sort'字段是如何'[null]':那是因爲'title.original'不存在 – Lol4t0

+0

不,當你使用一個不存在的字段時,你會得到一個異常。 – Tristan

+0

好的,您是否創建了映射_first_和_then_添加數據?還是相反?首先數據爲 – Lol4t0

回答

2

您沒有正確創建多字段。你應該做的是這樣的:

curl -XPOST "http://localhost:9200/movies/movie/_mapping" -d '{ 
    "movie": { 
     "properties": { 
     "director": { 
      "type": "string", 
      "fields": { 
       "original": {"type" : "string", "index" : "not_analyzed"} 
      } 
     } 
     } 
    } 
}' 

另外請注意,該教程中他們使用的聲明多領域,即用"type": "multi_field"的過時的方式。現在我們按照上面顯示的方式進行操作。

編輯形式註釋如下:更改映射到多字段後,您需要重新運行6個索引查詢重新索引六個電影,以便director.original字段被填充。

+0

我已通過正確的「multi_field」指令更正了我的問題。我也嘗試過你的非棄用方式來創建多字段,但我有完全相同的結果。 – Tristan

+0

您需要清除索引並重新索引數據。 – Val

+0

是的,它已經完成使用「curl -XDELETE」http:// localhost:9200/movies /「,然後重新創建整個索引(只是簡單的複製/粘貼教程,希望你可以在你自己的ES上試試這個)。 – Tristan

相關問題