ElasticSearch搜索查询有许多关系模型rails

时间:2016-05-14 17:21:42

标签: ruby-on-rails elasticsearch

我将ElasticSearch服务器用于搜索书籍,包括所有类型的书籍ISBN,书名和书籍标题。我已经使用三个具有关系的模型进行了索引编制,

预订hash_many additional_isbns,科目

预订hash_one别名

我的索引看起来像,

{
"_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"}]}}

我有像

这样的搜索方法
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')上搜索一样,搜索必须仅限于所有图书的主题值。 我如何只搜索主题但不搜索其他列?

2 个答案:

答案 0 :(得分:0)

以下是我在两个特定列中搜索对象的示例,只是为了让您了解如何添加查询。第二个问题是如何在弹性搜索中执行您的确切查询:

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

这将返回与AssemblyDistrict AD-13和'ALSO'selection_district ED-011匹配的前1000条记录。所以在你的情况下你会这样做:

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

'From'是偏移量,'size'是返回的最大数据集。此外,我看到许多人忽略的一个关键事项是,当您搜索数据时,您知道数据库中正在寻找的确切值,您可以使用过滤器而不是查询,它们会更快。如果你知道你需要与主题“一般”完全匹配而不是环形或靠近它的东西,下面将是如何使用过滤器。

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

答案 1 :(得分:0)

如果您要使用await查询,则可以将query_string default field更改为默认https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_default_field

_all

或者您可以使用{ query: { query_string: { query: query, default_operator: 'AND', default_field : 'subject.title' } } } https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_multi_field

fields

我想如果你将字段名称作为方法中的参数会更好。