简体   繁体   中英

Rails Elasticsearch, Searchkick

I just followed this tutorial to add elasticsearch to my app.

I work on a windows 8 system and I have elasticsearch installed. But when i try to search something in my app it doesn't find anything.

In the tutorial they say that I can use the rails console to see if the search is working.

I installed the searchkick gem and run bundle install.

I added searchkick to my Ticket class.

class Ticket < ActiveRecord::Base
  searchkick
  # ...
end

I added my Ticket class to the index

rake searchkick:reindex CLASS=Book

When I try to search something in the rails console it find nothing.

irb(main):001:0> results = Ticket.search("computer")
  Ticket Search (103.0ms)
curl http://lvh.me:9200/tickets_development/_search?pretty -d '{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "_all": {
              "query": "computer",
              "operator": "and",
              "boost": 10,
              "analyzer": "searchkick_search"
            }
          }
        },
        {
          "match": {
            "_all": {
              "query": "computer",
              "operator": "and",
              "boost": 10,
              "analyzer": "searchkick_search2"
            }
          }
        },
        {
          "match": {
            "_all": {
              "query": "computer",
              "operator": "and",
              "boost": 1,
              "fuzziness": 1,
              "max_expansions": 3,
              "analyzer": "searchkick_search"
            }
          }
        },
        {
          "match": {
            "_all": {
              "query": "computer",
              "operator": "and",
              "boost": 1,
              "fuzziness": 1,
              "max_expansions": 3,
              "analyzer": "searchkick_search2"
            }
          }
        }
      ]
    }
  },
  "size": 100000,
  "from": 0,
  "fields": []
}'

=> #<Searchkick::Results:0x6ec5de8 ,
@klass=Ticket(
  id: integer, 
  name: string, 
  client: string, 
  archived: boolean, 
  created_at: datetime, 
  updated_at: datetime
)
@response={
  "took"=> 90,
  "timed_out"=> false,
  "_shards"=> {
    "total"=> 5,
    "successful"=> 5,
    "failed"=> 0
  },
  "hits"=> {
    "total"=> 0,
    "max_score"=> nil,
    "hits"=> []
  }
},
@options={
  :page=>1,
  :per_page=>100000, 
  :padding=>0, 
  :load=>true, 
  :includes=>nil, 
  :json=>false
} > irb(main):002:0> results.map(&:name) => []

before running rake searchkick you gotta be sure elasticsearch is active.

sudo service elasticsearch status
sudo service elasticsearch start

After you run rake searchkick:reindex CLASS=Ticket Did you see any error?

I assume that you have a same issue like me, that is about table in database

please make sure below:

Table which you try to searching must has Primary key

If a table doesn't has Primary key Searchkick will try to reindex by ordering table's column which has a same name as your table name and It may has some problem like you, From my experience searchkick:reindex will notices some error after you run rake searchkick:reindex CLASS=your_model_class_name

Everytime you have edited table construct, Don't forget to run

rake searchkick:reindex CLASS=your_model_class_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM