2012-08-17 3 views
2

ElasticSearch의 클라이언트로 Tire gem을 사용하여 PDF 첨부 파일을 인덱싱하고 싶습니다.매핑되지 않은 필드는 ElasticSearch에서 반환 한 검색 결과에 포함됩니다.

mapping :_source => { :excludes => ['attachment_original'] } do 
    indexes :id, :type => 'integer' 
    indexes :folder_id, :type => 'integer' 
    indexes :attachment_file_name 
    indexes :attachment_updated_at, :type => 'date' 
    indexes :attachment_original, :type => 'attachment' 
end 

난 아직도 첨부 파일 내용이 검색에 포함 볼 수 있습니다 : 첨부 파일이 인덱스에 저장되지 않고 검색이 결과에 반환되지 않도록 내 매핑, 나는, _source에서 첨부 파일 필드를 제외 나는 다음과 같은 컬 명령을 실행할 때 결과 :

하지만 난 그냥뿐만 아니라 첨부 파일이 있음을 발견했습니다 :

curl -X POST "http://localhost:9200/user_files/user_file/_search?pretty=true" -d '{ 
    "query": { 
    "query_string": { 
     "query": "rspec" 
    } 
    } 
}' 

나는이 thread에 내 질문을 게시 검색 결과에 포함,하지만 당신은 여기에서 볼 수있는 매핑되지 않은 사람을 포함하여 다른 모든 필드는 또한 포함되어 있습니다

{ 
    "took": 20, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 0.025427073, 
    "hits": [ 
     { 
     "_index": "user_files", 
     "_type": "user_file", 
     "_id": "5", 
     "_score": 0.025427073, 
     "_source": { 
      "user_file": { 
      "id": 5, 
      "folder_id": 1, 
      "updated_at": "2012-08-16T11:32:41Z", 
      "attachment_file_size": 179895, 
      "attachment_updated_at": "2012-08-16T11:32:41Z", 
      "attachment_file_name": "hw4.pdf", 
      "attachment_content_type": "application/pdf", 
      "created_at": "2012-08-16T11:32:41Z", 
      "attachment_original": "JVBERi0xLjQKJeLjz9MKNyA" 
      } 
     } 
     } 
    ] 
    } 
} 

attachment_file_sizeattachment_content_type이 매핑에 정의되지 않은,하지만 반환됩니다 검색 결과 :

{ 
    "id": 5, 
    "folder_id": 1, 
    "updated_at": "2012-08-16T11:32:41Z", 
    "attachment_file_size": 179895, <--------------------- 
    "attachment_updated_at": "2012-08-16T11:32:41Z", 
    "attachment_file_name": "hw4.pdf", <------------------ 
    "attachment_content_type": "application/pdf", 
    "created_at": "2012-08-16T11:32:41Z", 
    "attachment_original": "JVBERi0xLjQKJeLjz9MKNyA" 
} 

여기 내 전체 구현의 :

include Tire::Model::Search 
    include Tire::Model::Callbacks 

    def self.search(folder, params) 
    tire.search() do 
     query { string params[:query], default_operator: "AND"} if params[:query].present? 
     #filter :term, folder_id: folder.id 
     #highlight :attachment_original, :options => {:tag => "<em>"} 
     raise to_curl 
    end 
    end 

    mapping :_source => { :excludes => ['attachment_original'] } do 
    indexes :id, :type => 'integer' 
    indexes :folder_id, :type => 'integer' 
    indexes :attachment_file_name 
    indexes :attachment_updated_at, :type => 'date' 
    indexes :attachment_original, :type => 'attachment' 
    end 

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

    def attachment_original 
    if attachment_file_name.present? 
     path_to_original = attachment.path 
     Base64.encode64(open(path_to_original) { |f| f.read }) 
    end  
    end 

누군가가 나를 왜 모든 필드을 알아내는 데 도움이 수 _source에 다시 포함 되나요?

편집 : 이것은 모든 필드가 매핑에 포함 된 몇 가지 이유로 볼 수 있듯이 localhost:9200/user_files/_mapping

{ 
    "user_files": { 
    "user_file": { 
     "_source": { 
     "excludes": [ 
      "attachment_original" 
     ] 
     }, 
     "properties": { 
     "attachment_content_type": { 
      "type": "string" 
     }, 
     "attachment_file_name": { 
      "type": "string" 
     }, 
     "attachment_file_size": { 
      "type": "long" 
     }, 
     "attachment_original": { 
      "type": "attachment", 
      "path": "full", 
      "fields": { 
      "attachment_original": { 
       "type": "string" 
      }, 
      "author": { 
       "type": "string" 
      }, 
      "title": { 
       "type": "string" 
      }, 
      "name": { 
       "type": "string" 
      }, 
      "date": { 
       "type": "date", 
       "format": "dateOptionalTime" 
      }, 
      "keywords": { 
       "type": "string" 
      }, 
      "content_type": { 
       "type": "string" 
      } 
      } 
     }, 
     "attachment_updated_at": { 
      "type": "date", 
      "format": "dateOptionalTime" 
     }, 
     "created_at": { 
      "type": "date", 
      "format": "dateOptionalTime" 
     }, 
     "folder_id": { 
      "type": "integer" 
     }, 
     "id": { 
      "type": "integer" 
     }, 
     "updated_at": { 
      "type": "date", 
      "format": "dateOptionalTime" 
     } 
     } 
    } 
    } 
} 

를 실행의 출력입니다!

+0

이 스레드에서 http://stackoverflow.com/questions/11251851/how-do-you-index-attachment-in-elasticsearch-with-tire?rq=1 정의되지 않은 필드가 맵핑. –

답변

1

to_indexed_json에는 attachment_original 메서드가 포함되어 있으므로 elasticsearch로 보내집니다. 이것이 다른 모든 속성이 매핑 및 결과적으로 소스에 포함되는 이유이기도합니다.

주제에 대한 자세한 내용은 ElasticSearch & Tire: Using Mapping and to_indexed_json 질문을 참조하십시오.

타이어가 실제로 JSON을 elasticsearch에 매핑하는 것 같습니다. 내 조언은 Tire.configure { logger STDERR, level: "debug" }을 사용하여 일어나는 일을 검사하고 trz가 원시 수준에서 문제를 정확하게 지적하는 것입니다.

+0

어떻게 to_indexed_json이 작동하는지 오해했습니다. 링크에 다시 한번 감사 드리며, 많은 도움이됩니다. –

관련 문제