【发布时间】:2015-06-25 07:40:22
【问题描述】:
我正在尝试使用弹性搜索术语查询对包含 URL 的字段进行搜索。我使用 elasticsearch-rails ActiveRecord 持久模式。这就是我尝试做到的方式。
total_views = UserAction.search :query=> {
:filtered=> {
:filter=> {
:term=> { action_path:"http://0.0.0.0:3000/tshirt/test" }
}
}
}
如果没有 '/' 或 ':' 字符,它可以工作。例如,当 action_path 只是“tshirt”时。其他字段不进行分析,如果字段中没有“/”、“:”等字符,它们将起作用。 所以显然弹性搜索试图分析它,但问题是它们不应该被分析,因为映射已经存在。
这是我的用户操作类
class UserAction
include Elasticsearch::Persistence::Model
extend Calculations
include Styles
attribute :user_id, Integer
attribute :user_referrer, String, mapping: { index: 'not_analyzed' }
attribute :user_ip, String, mapping: { index: 'not_analyzed' }
attribute :user_country, String, mapping: { index: 'not_analyzed' }
attribute :user_city, String, mapping: { index: 'not_analyzed' }
attribute :user_device, String, mapping: { index: 'not_analyzed' }
attribute :user_agent, String, mapping: { index: 'not_analyzed' }
attribute :user_platform
attribute :user_visitid, Integer
attribute :action_type, String, mapping: { index: 'not_analyzed' }
attribute :action_css, String, mapping: { index: 'not_analyzed' }
attribute :action_text, String, mapping: { index: 'not_analyzed' }
attribute :action_path, String, mapping: { index: 'not_analyzed' }
attribute :share_url, String, mapping: { index: 'not_analyzed' }
attribute :tag
attribute :date
我还尝试使用“mapping do..”添加索引,然后使用“create_index!”但结果是一样的。因为存在映射,所以它确实创建了映射。
这是我的 gem 文件
gem "elasticsearch-model", git: "git://github.com/elasticsearch/elasticsearch-rails.git", require: "elasticsearch/model"
gem "elasticsearch-persistence", git: "git://github.com/elasticsearch/elasticsearch-rails.git", require: "elasticsearch/persistence/model"
gem "elasticsearch-rails"
当我进行搜索时,我还看到那些未分析的字段。
:reload_on_failure=>false,
:randomize_hosts=>false,
:transport_options=>{}},
@protocol="http",
@reload_after=10000,
@resurrect_after=60,
@serializer=
#<Elasticsearch::Transport::Transport::Serializer::MultiJson:0x007fc4bf9e0e18
@transport=#<Elasticsearch::Transport::Transport::HTTP::Faraday:0x007fc4bf9b35a8 ...>>,
@sniffer=
#<Elasticsearch::Transport::Transport::Sniffer:0x007fc4bf9e0dc8
@timeout=1,
@transport=#<Elasticsearch::Transport::Transport::HTTP::Faraday:0x007fc4bf9b35a8 ...>>,
@tracer=nil>>,
@document_type="user_action",
@index_name="useraction",
@klass=UserAction,
@mapping=
#<Elasticsearch::Model::Indexing::Mappings:0x007fc4bfab18d8
@mapping=
{:created_at=>{:type=>"date"},
:updated_at=>{:type=>"date"},
:user_id=>{:type=>"integer"},
:user_referrer=>{:type=>"string"},
:user_ip=>{:type=>"string"},
:user_country=>{:type=>"string", :index=>"not_analyzed"},
:user_city=>{:type=>"string", :index=>"not_analyzed"},
:user_device=>{:type=>"string", :index=>"not_analyzed"},
:user_agent=>{:type=>"string", :index=>"not_analyzed"},
:user_platform=>{:type=>"string"},
:user_visitid=>{:type=>"integer"},
:action_type=>{:type=>"string", :index=>"not_analyzed"},
:action_css=>{:type=>"string", :index=>"not_analyzed"},
:action_text=>{:type=>"string", :index=>"not_analyzed"},
:action_path=>{:type=>"string", :index=>"not_analyzed"}},
@options={},
@type="user_action">,
@options={:host=>UserAction}>,
@response={"took"=>1, "timed_out"=>false, "_shards"=>{"total"=>4, "successful"=>4, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}>
(END)
初始化文件只有 elastichq 连接 url。
elastichq 中有数据,所以我应该得到结果,但没有得到任何结果。
user_action 1 AUzH9xKDueQ8OtBQuyQC http://example.org/api/analytics/track
user_actions user_action 1 AUzIAUsvueQ8OtBQuyQg http://0.0.0.0:3000/tshirt/funnel_test2
user_actions user_action 1 AUzH7ay5ueQ8OtBQuyP2 http://example.org/api/analytics/track
user_actions user_action 1 AUzH-HAdueQ8OtBQuyQU http://0.0.0.0:3000/tshirt/test
user_actions user_action 1 AUzIJbCGueQ8OtBQuyQ4 http://example.org/api/analytics/track
user_actions user_action 1 AUzIJbCjueQ8OtBQuyQ5 http://example.org/api/analytics/track
来自 Elastichq 的卷曲结果
curl -XGET "https://YYYYY:XXXXX@xxxx.qbox.io/user_actions/_mapping"
{
"user_actions": {
"mappings": {
"user_action": {
"properties": {
"action_css": { "type": "string" },
"action_path": { "type": "string" },
"action_text": { "type": "string" },
"action_type": { "type": "string" },
"created_at": { "format": "dateOptionalTime", "type": "date" },
"date": { "type": "string" },
"share_url": { "type": "string" },
"tag": { "type": "string" },
"updated_at": { "format": "dateOptionalTime", "type": "date" },
"user_agent": { "type": "string" },
"user_city": { "type": "string" },
"user_country": { "type": "string" },
"user_device": { "type": "string" },
"user_id": { "type": "long" },
"user_ip": { "type": "string" },
"user_referrer": { "type": "string" },
"user_visitid": { "type": "long" }
}
}
}
}
}
任何人都可以帮助我进行 url 术语搜索工作吗?
【问题讨论】:
-
创建的映射(查看 /_mapping?pretty)是否符合您的预期?
-
抱歉没明白你的意思
-
prntscr.com/6v7aqp 这是 elastichq 上的映射部分...我不知道我是否应该在这里看到未分析的字段,但它们不在这里。但正如我上面写的,我在搜索时看到了响应的映射。
-
您在查询时看到的映射不是响应的一部分 - ES 不返回该信息。我不确定 elastichq 如何显示映射信息,但我希望看到诸如分析器之类的详细信息 - 看起来它可能是动态映射(如果您在设置映射之前插入数据会发生这种情况)
-
你能运行
curl -XGET "http://localhost:9200/useraction/user_action/_mapping"并提供输出吗?
标签: ruby ruby-on-rails-4 elasticsearch elasticsearch-rails