我经营一个摄影网站。摄影师把他们的"by_line“写成”某某名字/网站“。
我在大多数查询中都使用了DSL,使用"terms“将"filter”查询串在一起,这对于90%的情况都很好,但是在这种情况下,我得到的查询结果为零(请注意我是如何试图避开正斜杠的):
body:
{query:
{filtered:
{filter:
{and:
[{term: {is_visible: true}},
{term: {"event.by_line": "john\\/my_website.com"}}]
}
}
}
}我正在使用elasticsearch版本1.5.2
当我查看这个URL上的映射时
http://127.0.0.1:9200/development_photos/_mapping?pretty=1这是一个示例记录
{
"_index" : "development_photos",
"_type" : "photo",
"_id" : "251",
"_score" : 1.0,
"_source":{"id":251,
"name":"LET'S PANIC ISSUE 02 LAUNCH DINNER",
"image":"BFA_85_251.jpg",
"position":null,
"event_id":85,
"created_at":"2015-06-21T22:27:21.000Z",
"is_visible":true,
"img":{"thumb":"thumb.png"}
,"ppl":[{"id":429,"position":20,"person_name":"John Kealy","person_slug":"john-kealy","person_id":30}],
"keywords":[],
"event":{"id":85,
"state":"New York",
"time_created":"8:00 PM",
"date_created":"20150611",
"city":"New York",
"caption":"Let's Panic-mosphere",
"by_line":"John Kealy/BFA.com",
"name":"LET'S PANIC ISSUE 02 LAUNCH DINNER",
"zip_processing":null
}
}
}映射
{
"development_photos" : {
"mappings" : {
"photo" : {
"properties" : {
"created_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"event" : {
"properties" : {
"abstract" : {
"type" : "string"
},
"author" : {
"type" : "string"
},
"by_line" : {
"type" : "string"
},
"caption" : {
"type" : "string"
},
"city" : {
"type" : "string"
},
"copyright_notice" : {
"type" : "string"
},
"country_primary_location_name" : {
"type" : "string"
},
"cover_photo_id" : {
"type" : "long"
},
"created_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"date_created" : {
"type" : "string"
},
"folder" : {
"type" : "string"
},
"id" : {
"type" : "long"
},
"is_private" : {
"type" : "boolean"
},
"make_public_on" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
"password" : {
"type" : "string"
},
"position" : {
"type" : "long"
},
"pr_usage" : {
"type" : "boolean"
},
"province" : {
"type" : "string"
},
"purchases_disabled" : {
"type" : "boolean"
},
"state" : {
"type" : "string"
},
"sub_location" : {
"type" : "string"
},
"time_created" : {
"type" : "string"
},
"updated_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"uploader_id" : {
"type" : "long"
},
"view_only_password" : {
"type" : "string"
}
}
},
"event_id" : {
"type" : "long"
},
"id" : {
"type" : "long"
},
"image" : {
"type" : "string"
},
"img" : {
"properties" : {
"preview" : {
"type" : "string"
},
"thumb" : {
"type" : "string"
},
"thumb_rollover" : {
"type" : "string"
}
}
},
"is_visible" : {
"type" : "boolean"
},
"keywords" : {
"properties" : {
"id" : {
"type" : "long"
},
"name" : {
"type" : "string"
},
"tag_id" : {
"type" : "long"
}
}
},
"name" : {
"type" : "string"
},
"position" : {
"type" : "long"
},
"ppl" : {
"properties" : {
"id" : {
"type" : "long"
},
"person_id" : {
"type" : "long"
},
"person_name" : {
"type" : "string"
},
"person_slug" : {
"type" : "string"
},
"position" : {
"type" : "long"
}
}
}
}
}
}
}
}根据bittusarkar的建议,我更新了我的查询,以使用fquery。现在我还有两个问题。如何链接fquery查询?
这将导致弹性搜索失败。
{filtered:
{filter:
{and:
[{term:{is_visible:true}},
{term:{is_private:false}}],
fquery:
{query:
{match:
{by_line:"Kealy/BFAnyc",
sub_location:"can i chain these queries"}}}}}}这会导致错误“查询以简化的形式解析,具有直接字段名,但包含的选项比字段名更多,可能使用它的' options‘表单,并带有'query’元素?”
此外,当我尝试将fquery与我原来的只链接“和”过滤器的方式混合时,它会返回不应该使用的结果。它似乎忽略了“和”查询。例如:
{filtered:
{filter:
{and:
[{term: {is_visible:true}},
{term: {is_private: false}},
{term: {sub_location: "can"}},
{term: {sub_location: "i"}},
{term: {sub_location: "chain"}},
{term: {sub_location: "these"}},
{term: {sub_location: "queries"}}],
fquery:
{query:
{match:
{by_line:JohnKealy/BFAnyc"}}}}}}这样的记录出现在搜索结果中:(注意"sub_location与我的sub_location查询不匹配“)
{event: {"id":1,
"sub_location":"New York",
"state":nil,
"author":nil,
"copyright_notice":nil,
"country_primary_location_name":nil,
"time_created":nil,
"date_created":nil,
"city":nil,
"caption":nil,
"by_line":"JohnKealy/BFAnyc",
"abstract":nil,
"name":"John Kealy",
"province":nil,
"folder":"foo",
"password":nil,
"visible":nil,
"zip_created_at":nil,
"zip_processing":nil,
"position":0,
"pdf":nil,
"cover_photo_id":nil,
"created_at":"2015-07-16T15:53:26.000Z",
"updated_at":"2015-07-16T15:53:26.000Z",
"is_private":false,
"price_mod":nil,
"uploader_id":nil,
"view_only_password":nil,
"pr_usage":nil,
"purchases_disabled":nil,
"make_public_on":nil}}发布于 2015-07-16 04:06:42
从您的映射中可以看出,您正在为by_line字段使用默认分析器。这意味着,"John /BFA.com“的值分别作为以下术语进行索引-- "john”、"kealy“、"bfa”和"com“。现在,term查询适用于未分析的字段。它正在搜索完整的术语"John /BFA.com“,当然,这个词在倒排索引中不存在。这里需要使用match查询而不是term查询,如下所示:
{
"query": {
"match": {
"by_line": "John Kealy/BFA.com"
}
}
}如果希望将其作为筛选器的一部分,可以使用fquery,如下所示:
{
"filter": {
"fquery": {
"query": {
"match": {
"by_line": "John Kealy/BFA.com"
}
},
"_cache": true
}
}
}https://stackoverflow.com/questions/31442499
复制相似问题