Elasticsearch-rails多匹配查询不起作用

时间:2017-12-20 04:46:35

标签: ruby-on-rails ruby elasticsearch elasticsearch-rails

我是关于ES的病。什么都行不通。据说这是ES的下一个问题。

我使用的是elasticsearch-rails gem。

当我运行curl命令时,我可以使用multi_match进行查询并得到预期的结果:

n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
isosurface(F,0)
lighting phong
axis equal

使用执行相同操作的ruby代码,我得到0结果。我不明白。

地址模型:

curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "multi_match" : {
      "query":    "NY", 
      "fields": [ "street", "state" ] 
    }
  }
}
'
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.47000363,
    "hits" : [
      {
        "_index" : "addresses",
        "_type" : "address",
        "_id" : "1",
        "_score" : 0.47000363,
        "_source" : {
          "street" : "Somewhere Else Rd",
          "city" : "Somewhere",
           "state" : "NY",
          "zip" : "88329"
        }
      },
      {
        "_index" : "addresses",
        "_type" : "address",
        "_id" : "2",
        "_score" : 0.47000363,
        "_source" : {
          "street" : "Somewhere Drive",
          "city" : "Somewhere",
          "state" : "NY",
          "zip" : "42293"
        }
      }
    ]
  }
} 

控制器操作:

settings index: { number_of_shards: 1 } do
  mappings dynamic: 'false' do
    indexes :street, analyzer: 'keyword'
    indexes :city, analyzer: 'keyword'
    indexes :state, analyzer: 'keyword' 
    indexes :zip, analyzer: 'keyword'
  end
end

def as_indexed_json(options={})
    as_json(
        only: [:street, :city, :state, :zip]
    )
 end

每次调用搜索功能时,我重新编制索引的唯一原因仅用于调试目的。这是控制台输出。

 def search
    Address.all.import force: true
    @addresses = Address.all
    puts @addresses.size

     @addresses = Address.search(
           query: { 
             multi_match: {
                 query: "NY",
                 fields: ['street', 'state']
             }
          }
          )
          puts @addresses.size
end

1 个答案:

答案 0 :(得分:0)

这件事发生在我身上,这与我试图用错误的数据类型(在这种情况下是关键字)声明索引的事实有关。我认为你不需要像那样声明你的索引,因为你需要的东西似乎非常标准。

尝试删除整个settings块,然后删除索引

Model.__elasticsearch__.delete_index!

重新创建索引

Model.__elasticsearch__.create_index!

索引您的元素

instance.__elasticsearch__.index_document

并搜索

这是一个适合我的多元素样本:

  def as_indexed_json(options={})
    h = {
      "id" => id,
      "author_name" => author_name,
      "author_email" => author_email,
      "content_title" => content_title,
      "content" => content,
      "circle_name" => circle_name.downcase,
      "updated_at" => updated_at.to_i
    }
  end

然后搜索:

  search_params = {
    query: {
      multi_match: {
        query: query.downcase,
        fields: ["author_name", "author_email", "content_title", "content", "circle_name"]
      }
    },
    sort: [
      { updated_at: {order: "desc"}},
      "_score"
    ],
  }

  Post.__elasticsearch__.search(search_params).results

也许会有所帮助