小编典典

在Elasticsearch中转义斜线

elasticsearch

我经营一个摄影网站。摄影师将其“ 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"}}}}}}

这将导致错误“查询以简化的形式解析,具有直接的字段名称,但除字段名称之外还包含更多选项,可能使用带有’query’元素的’options’形式?”。

此外,当我尝试将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}}

阅读 1976

收藏
2020-06-22

共1个答案

小编典典

从您的映射看来,您正在使用默认的分析器作为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
      }
   }
}
2020-06-22