作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法在 Rails 应用程序中突出显示 Elasticsearch(和 Tire)。我可以成功索引 PDF 附件并查询它们,但我无法使突出显示工作。
对 ES 不是很熟悉,所以不确定去哪里进行故障排除。将从映射和 curl 查询开始,但请随时询问更多信息。
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
映射(通过 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"
}
}
}
}
}%
在 125 页索引 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"
}
} ]
}
}%
我期待这样的部分:
"highlight" : {
"attachment" : [ "\nLast Year <em>Xerox</em> moved their facilities" ]
}
感谢您的帮助!
Edit2: 调整后的查询(将 attachment
更改为 attachment.file
)无济于事:
$ 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(删除“字段”):
$ 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(从操作教程中的附件类型映射):
$ 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"
}
}
}
}
}
}
}
最佳答案
我想通了!终于……
问题出在我在 Article 类中的映射语法上。需要将“:file”重命名为“: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' }
}
关于ruby-on-rails - Elasticsearch:突出显示附件中的命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12170497/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!