0

我快厌倦ES了。没有任何工作。话虽如此,这是我与 ES 的下一个问题。

我正在使用 elasticsearch-rails gem。

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

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

使用做同样事情的 ruby​​ 代码,我得到 0 个结果。我不明白。

地址型号:

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

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

Started GET "/addresses/search?q=1" for 75.65.92.24 at 2017-12-20 04:35:02 +0000
Cannot render console from 75.65.92.24! Allowed networks: 127.0.0.1, ::1, 
127.0.0.0/127.255.255.255
Processing by AddressesController#search as */*
  Parameters: {"q"=>"1"}
  Address Load (0.2ms)  SELECT  "addresses".* FROM "addresses" ORDER BY 
"addresses"."id" ASC LIMIT ?  [["LIMIT", 1000]]
  CACHE Address Load (0.0ms)  SELECT  "addresses".* FROM "addresses" ORDER 
BY "addresses"."id" ASC LIMIT ?  [["LIMIT", 1000]]
   (0.2ms)  SELECT COUNT(*) FROM "addresses"
3
0
  Rendering addresses/search.js.erb
  Rendered collection of addresses/_address.html.erb [0 times] (0.0ms)
  Rendered addresses/search.js.erb (4.0ms)
Completed 200 OK in 334ms (Views: 8.8ms | ActiveRecord: 1.4ms)
4

1 回答 1

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

也许会有所帮助

于 2017-12-20T04:59:58.197 回答