2016-05-14 68 views
2

我正在使用ElasticSearch服務器搜索與所有類型的圖書的書籍ISBN,圖書標題和圖書的主題標題。我做了使用的三種模式具有關係一樣,ElasticSearch搜索查詢具有許多關係模型rails

書hash_many additional_isbns索引,科目

書hash_one aliase

我的索引樣子,

{ 
"_index":"books","_type":"book","_id":"9781135028411","_score":0.6377245, 
    "_source":{"book_vbid": "9781135028411","book_title": "Objectivity, Science and Society","book_author":"Paul A Komesaroff", 
       "additional_isbn": [{"isbn_value": "0415474876"},{"isbn_value": "9780415474870"}], 
       "aliase": {"eisbn_canonical":"9781135028411","isbn_canonical":"9781135028411","print_isbn_canonical":"9780415474870","isbn13":"9781135028411","isbn10":"1135028419"}, 
       "subjects": [{"title": "General"},{"title": "General"},{"title": "General"},{"title": "Physical & Earth Sciences -> Science -> General"},{"title": "Social Sciences -> Philosophy -> General"},{"title": "Social Sciences -> Social Sciences -> General"}]}},{ 
"_index":"books","_type":"book","_id":"9781135027896","_score":0.6348529, 
    "_source":{"book_vbid": "9781135027896","book_title": "Beyond Empiricism","book_author":"Andrew Tudor", 
       "additional_isbn": [{"isbn_value": "0415475007"},{"isbn_value": "9780415475006"}], 
       "aliase": {"eisbn_canonical":"9781135027896","isbn_canonical":"9781135027896","print_isbn_canonical":"9780415475006","isbn13":"9781135027896","isbn10":"1135027897"}, 
       "subjects": [{"title": "General"},{"title": "General"},{"title": "General"},{"title": "Physical & Earth Sciences -> Science -> General"},{"title": "Social Sciences -> Philosophy -> General"},{"title": "Social Sciences -> Social Sciences -> General"}]}},{ 
"_index":"books","_type":"book","_id":"9781134429059","_score":0.6258716, 
    "_source":{"book_vbid": "9781134429059","book_title": "Your Murderer","book_author":"Vassily Aksyonov", 
       "additional_isbn": [{"isbn_value": "9057551039"},{"isbn_value": "9789057551031"}], 
       "aliase": {"eisbn_canonical":"9781134429059","isbn_canonical":"9781134429059","print_isbn_canonical":"9789057551031","isbn13":"9781134429059","isbn10":"1134429053"}, 
       "subjects": [{"title": "General"},{"title": "General"},{"title": "Humanities -> Performing Arts -> General"},{"title": "Humanities -> TheatrGeneral"}]}} 

我有搜索方法like,

def self.search(query, options = {}) 
es_options = 
    { 
    query: { 
     query_string: { 
     query:   query, 
     default_operator: 'AND', 
    } 
    }, 
    sort: '_score', 
}.merge!(options) 
__elasticsearch__.search(es_options) end 

我的要求是就像我在Book.search('general')上搜索時一樣,搜索必須只針對所有書籍的主題值。 我只能在主題上搜索,而不能在其他列上搜索?

回答

0

以下是我如何在兩個特定列中搜索對象的示例,只是爲了讓您更好地瞭解如何添加查詢。第二個查詢是你會怎麼做您的確切查詢彈性搜索:

User.search(from: 0, size: 1000, query: { bool: { must: [ {match: { assembly_district: "AD\-13"}}, {match: { election_district: "ED\-011"}} ] } }).records.last 

這將返回第1000條記錄AssemblyDistrict AD-13和「也」 election_district ED-011匹配。所以在你的情況下,你會這樣做:

Book.search(query: { bool: { must: [ {match: { subject: "general"}} ] } }).results 

'From'是偏移量,'size'是返回的最大數據集。另外,我發現許多人忽視的一個關鍵問題是,當您在數據庫中搜索數據時,如果您知道數據庫中要查找的確切值,則可以使用「過濾器」而不是「查詢」,它們將更快。以下是如何使用過濾器,如果你知道你需要一個精確匹配的主題「一般」,而不是迂迴或靠近它。

Book.search(query: { bool: { filter: [ {term: { subject: "general"}} ] } }).results 
+0

謝謝你的迴應。所以我有要求像我想只搜索所有書籍的所有主題的標題,只需要結果。 – rick

+0

哦,我明白了,我現在正在從手機上打字,但我會建議嘗試將中間的查詢更改爲主題:{title:'general'}而不是主題:「general」。當我回到我的比賽中時,我會更深入地看。 – bkunzi01

0

如果要使用query_string查詢要麼你可以改變default field這是_all爲默認https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_default_field

{ 
    query: { 
     query_string: { 
      query:   query, 
      default_operator: 'AND', 
      default_field : 'subject.title' 
     } 
    } 
} 

,或者您可以使用fieldshttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_multi_field

{ 
    query: { 
     query_string: { 
      query:   query, 
      default_operator: 'AND', 
      fields : ['subject.title'] 
     } 
    } 
} 

我想這將如果您將字段名稱作爲您的方法中的參數,則會更好。

+0

我只需要搜索主題的標題。所以,我可以使查詢像主題[:標題]意味着它將搜索只有主題的'標題'字段 – rick

+0

你可以使[「subject.title」] – alpert