Mac*_*eek 2 ruby ruby-on-rails elasticsearch tire
我已将相关字段映射为多字段.我已经设置了'名称'属性,使用雪球分析仪进行分析,并将'精确'设置为未分析.我可以在这个字段上搜索并过滤.我无法对这个领域进行排序.尝试排序时,弹性返回错误"无法对每个文档具有多个值的字符串类型进行排序,或者每个字段使用多个标记".
我试图创建一个名为'raw'的附加字段,类似于exact,这也不起作用.下面是我的映射以及我如何尝试通过轮胎gem进行排序:
mapping do
indexes :sectors, :type => 'object',
:properties => { :name => { :type => 'multi_field',
:fields => {
:name => { :type => 'string', :analyzer => 'snowball' },
:exact => { :type => 'string', :index => 'not_analyzed' , :include_in_all => false }
}
}
}
end
def to_indexed_json
to_json( :include => {
...
:sectors => { :only => ["name"] },
...
})
end
def self.search(params)
tire.search(:load => true, :page=>params[:page], :per_page => 12) do
if params[:query].present?
query { string params[:query], :default_operator => "OR" }
else
query { all }
end
filter :term, "sectors.name.exact" => params[:sector] if params[:sector].present?
sort { by 'sectors.name.exact','asc' } if !params[:sort][:sector].blank?
end
end
Run Code Online (Sandbox Code Playgroud)
您收到该错误的原因基本上有两个:
name.exact字段不同,因为它not_analyzed位于您的映射中name字段添加多个值,例如在索引时提供值数组.我会查看你的文件是什么样的.为了快速完成,您可以根据脚本制作术语构面,该脚本返回name.exact字段中包含的项目数,如下所示.如果您至少返回其中一个小于1的小平面条目,则表示您已向该字段添加了多个值至少一次,因此您无法对其进行排序.
{
"query" : {
"match_all" : {}
},
"facets" : {
"my_facet" : {
"terms" : {
"script_field" : "doc['name.exact'].values.length"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1372 次 |
| 最近记录: |