我快厌倦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)