2012-08-29 10 views
7

Mam problem z włączeniem podświetlania do pracy z Elasticsearch (i Tyre) w aplikacji Rails. Mogę z powodzeniem indeksować załączniki do plików PDF i je sprawdzać, ale nie mogę włączyć podświetlania.Elasticsearch: Podświetlanie trafień z załącznika

Niezbyt dobrze znany z ES, więc nie wiesz, gdzie szukać rozwiązywania problemów. Rozpocznie się od odwzorowań i zapytania zwijania, ale możesz poprosić o więcej informacji.

class Article < ActiveRecord::Base 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 

    attr_accessible :title, :content, :published_on, :filename 

    mapping do 
    indexes :id, :type =>'integer' 
    indexes :title 
    indexes :content 
    indexes :published_on, :type => 'date' 
    indexes :attachment, :type => 'attachment', 
          :fields => { 
          :name  => { :store => 'yes' }, 
          :content => { :store => 'yes' }, 
          :title  => { :store => 'yes' }, 
          :file  => { :term_vector => 'with_positions_offsets', :store => 'yes' }, 
          :date  => { :store => 'yes' } 
          } 
    end 

    def to_indexed_json 
    to_json(:methods => [:attachment]) 
    end 

    def attachment 
    if filename.present? 
     path_to_pdf = "/Volumes/Calvin/sample_pdfs/#{filename}.pdf" 
     Base64.encode64(open(path_to_pdf) { |pdf| pdf.read }) 
    else 
     Base64.encode64("missing") 
    end 
    end 
end 

Mappings (przez Curl):

$ curl -XGET 'http://localhost:9200/_mapping?pretty=true' 
{ 
    "articles" : { 
    "article" : { 
     "properties" : { 
     "attachment" : { 
      "type" : "attachment", 
      "path" : "full", 
      "fields" : { 
      "attachment" : { 
       "type" : "string" 
      }, 
      "title" : { 
       "type" : "string", 
       "store" : "yes" 
      }, 
      "name" : { 
       "type" : "string", 
       "store" : "yes" 
      }, 
      "date" : { 
       "type" : "date", 
       "ignore_malformed" : false, 
       "store" : "yes", 
       "format" : "dateOptionalTime" 
      }, 
      "content_type" : { 
       "type" : "string" 
      } 
      } 
     }, 
     "content" : { 
      "type" : "string" 
     }, 
     "created_at" : { 
      "type" : "date", 
      "ignore_malformed" : false, 
      "format" : "dateOptionalTime" 
     }, 
     "filename" : { 
      "type" : "string" 
     }, 
     "id" : { 
      "type" : "integer", 
      "ignore_malformed" : false 
     }, 
     "published_on" : { 
      "type" : "date", 
      "ignore_malformed" : false, 
      "format" : "dateOptionalTime" 
     }, 
     "title" : { 
      "type" : "string" 
     }, 
     "updated_at" : { 
      "type" : "date", 
      "ignore_malformed" : false, 
      "format" : "dateOptionalTime" 
     } 
     } 
    } 
    } 
}% 

zapytanie z 'trafienie' wewnątrz 125 widoku indeksowanego PDF:

$ curl "localhost:9200/_search?pretty=true" -d '{ 
quote> "fields" : ["title"], 
quote> "query" : { 
quote>  "query_string" : { 
quote>  "query" : "xerox" 
quote>  } 
quote> }, 
quote> "highlight" : { 
quote>  "fields" : { 
quote>  "attachment" : {} 
quote>  } 
quote> } 
quote> }' 

{ 
    "took" : 1077, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 0.036417194, 
    "hits" : [ { 
     "_index" : "articles", 
     "_type" : "article", 
     "_id" : "13", 
     "_score" : 0.036417194, 
     "fields" : { 
     "title" : "F-E12" 
     } 
    } ] 
    } 
}%  

Spodziewałem się sekcję jak:

"highlight" : { 
     "attachment" : [ "\nLast Year <em>Xerox</em> moved their facilities" ] 
    } 

Dzięki za wszelką pomoc!

Edit2: zapytania skorygowana (zmiana attachment do attachment.file) bezskutecznie:

$ curl "localhost:9200/_search?pretty=true" -d '{ 
    "fields" : ["title","attachment"], 
    "query" : {"query_string" : {"query" : "xerox"}}, 
    "highlight" : {"fields" : {"attachment.file" : {}}} 
}' 

{ 
    "took" : 221, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 0.036417194, 
    "hits" : [ { 
     "_index" : "articles", 
     "_type" : "article", 
     "_id" : "13", 
     "_score" : 0.036417194, 
     "fields" : { 
     "title" : "F-E12", 
     "attachment" : "JVBERi0xLjYNJeLjz9MNCjk4NSAwIG9iag08PC9MaW5lYXJpemVkIDEvTCA...\n" 
     } 
    } ] 
    } 
} 

Edit3 (usuń "pól"):

$ curl "localhost:9200/_search?pretty=true" -d '{ 
> "query" : {"query_string" : {"query" : "xerox"}}, 
> "highlight" : {"fields" : {"attachment" : {}}} 
> }' 

{ 
    "took" : 1078, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 0.036417194, 
    "hits" : [ { 
     "_index" : "articles", 
     "_type" : "article", 
     "_id" : "13", 
     "_score" : 0.036417194, "_source" : {"content":"Real report","created_at":"2012-08-28T22:44:08Z","filename":"F-E12","id":13,"published_on":"2007-12-28","title":"F-E12","updated_at":"2012-08-28T22:44:08Z","attachment":"JVBERi0xLjYNJeLjz9MNCjk4NSAwIG9iag08PC9MaW5lYXJpemVkID...\n" 
     } 
    } ] 
    } 
} 

Edit4 (mapowanie od typu załącznika w działaniu samouczek):

$ curl -XGET 'http://localhost:9200/test/_mapping?pretty=true' 
{ 
    "test" : { 
    "attachment" : { 
     "properties" : { 
     "file" : { 
      "type" : "attachment", 
      "path" : "full", 
      "fields" : { 
      "file" : {    #<== This appears to be missing 
       "type" : "string",  #<== from my Articles mapping 
       "store" : "yes",  #<== 
       "term_vector" : "with_positions_offsets" #<== 
      }, 
      "author" : { 
       "type" : "string" 
      }, 
      "title" : { 
       "type" : "string", 
       "store" : "yes" 
      }, 
      "name" : { 
       "type" : "string" 
      }, 
      "date" : { 
       "type" : "date", 
       "ignore_malformed" : false, 
       "format" : "dateOptionalTime" 
      }, 
      "keywords" : { 
       "type" : "string" 
      }, 
      "content_type" : { 
       "type" : "string" 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

Obawiam się, że opublikowane przez ciebie mapowanie jest związane z innym typem: jest to załącznik, a nie artykuł. Czy jesteś pewien, że mapowanie artykułu jest poprawne? Czy możesz również dodać samo przyłączenie pola do wyjścia? – javanna

+0

Nie jestem pewien * wszystko * jest poprawne, więc proszę o sugestie. Dodałem metodę "zajęcia", o którą prosiłeś. Doceniam Twoją pomoc! – Meltemi

+0

Niestety, nie zauważyłem, że używasz pola typu załącznika! Mapowanie wygląda dobrze! Myślę, że powinieneś spróbować zaznaczyć pole 'attachment.file' zamiast' attachment'. Daj mi znać, jak poszło! – javanna

Odpowiedz

8

Rozgryzłem to! Wreszcie ...

Problem był z moją składnię mapowania w klasie Article. Potrzebny do zmiany nazwy ": file" na ": attachment".

tire.mapping do 
    indexes :id, :type =>'integer' 
    indexes :title 
    indexes :content 
    indexes :published_on, :type => 'date' 
    indexes :attachment, :type => 'attachment', #:null_value => 'missing_file', 
          :fields => { 
          :name  => { :store => 'yes' }, # exists?!? 
          :content => { :store => 'yes' }, 
          :title  => { :store => 'yes' }, 
    # WRONG! see next line => :file  => { :term_vector => 'with_positions_offsets', :store => 'yes' }, 
          :attachment => { :term_vector => 'with_positions_offsets', :store => 'yes' }, 
          :date  => { :store => 'yes' } 
          } 
tire.mapping do 
    indexes :id, :type =>'integer' 
    indexes :title 
    indexes :content 
    indexes :published_on, :type => 'date' 
    indexes :attachment, :type => 'attachment', #:null_value => 'missing_file', 
          :fields => { 
          :name  => { :store => 'yes' }, # exists?!? 
          :content => { :store => 'yes' }, 
          :title  => { :store => 'yes' }, 
    # WRONG! see next line => :file  => { :term_vector => 'with_positions_offsets', :store => 'yes' }, 
          :attachment => { :term_vector => 'with_positions_offsets', :store => 'yes' }, 
          :date  => { :store => 'yes' } 
          } 
+1

Niezły chwyt! Cieszę się, że cię rozwiązałeś! – javanna

+0

Wyobrażam sobie, że to się zmieniło (z powrotem) na "plik" od wersji ES 0.90.0 – divadpoc

+0

Każdy pomysł, jak to zrobić za pomocą curl lub python? – Craneum

Powiązane problemy