gpt4 book ai didi

elasticsearch - 在 Elasticsearch 中转义斜杠

转载 作者:行者123 更新时间:2023-12-02 22:20:15 25 4
gpt4 key购买 nike

我经营一个摄影网站。摄影师把他们的“by_line”写成“Some name/ourwebsite.com”。

我有一个 DSL 用于我的大部分查询,使用“术语”将“过滤”查询串在一起,这对 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 查询?

这将导致 Elasticsearch 失败

{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"}}}}}}

这会导致错误“以简化形式解析的查询,具有直接字段名称,但包含的选项不仅仅是字段名称,可能使用其‘选项’形式,带有‘查询’元素?”

此外,当我尝试将 fquery 与仅链接“and”过滤器的旧方法混合使用时,它会返回不应返回的结果。它似乎忽略了“and”查询。例如:

{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}}

最佳答案

从您的映射看来,您正在为 by_line 字段使用默认分析器。这意味着值“John Kealy/BFA.com”被分别索引为以下术语 - “john”、“kealy”、“bfa”和“com”。现在 term 查询适用于非分析字段。它正在搜索完整的术语“John Kealy/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
}
}
}

关于elasticsearch - 在 Elasticsearch 中转义斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31442499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com