在弹性搜索中逃避斜线 [英] Escaping slash in elasticsearch

查看:92
本文介绍了在弹性搜索中逃避斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经营摄影网站。摄影师把他们的by_line作为Some name / ourwebsite.com。



我有一个DSL我用于我的大部分查询,使用条款串联在一起过滤查询,这对90%的情况非常有用,但是这种情况我用这样的查询得到零结果(注意我试图逃避正斜杠):

 身体:
{查询:
{过滤:
{过滤器:
{和:
[{term:{is_visible:true}},
{期限:{event.by_line:john\\\ / my_website.com}}]
}
}
}
}

我正在使用弹性搜索版本1.5.2



当我看此URL的映射

  http://127.0.0.1:9200/development_photos/_mapping?pretty=1 

这是一个示例记录

 _index:development_photos,
_type:photo,
_id:251,
_score
_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:{
属性:{
abstract:{
type:string
},
author:{
type:string
},
by_line:{
type:string
},
caption:{
type:字符串
},
city:{
type:string
},
copyright_notice:{
type :string
},
country_primar y_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 b $ bformat: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:{
键入: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:{
property:{
id:{
type:long
},
person_id:{
type long
},
person_name:{
type:string
},
person_slug:{
type :string
},
position:{
type:long
}
}
}
}
}
}
}
}

根据bittusarkar的建议,我更新了我的查询给我们有趣现在我还有两个问题。如何链接fquery查询?



这将导致弹性搜索失败

  {filtered: 
{filter:
{和:
[{term:{is_visible:true}},
{term:{is_private:false}}],
fquery:
{query:
{match:
{by_line:Kealy / BFAnyc,
sub_location:我可以链接这些查询}}}}}}

这样会导致错误
查询以简化的形式解析,直接字段名称,但包含更多选项而不仅仅是字段名称,可能使用'options'表单和'query'元素?



此外,当我尝试将fquery与我旧的方式链接和过滤器,它返回不应该的结果..它似乎忽略了和查询。例如:

  {filters:
{filter:
{and:
[这个术语:{is_visible:true}},
{term:{is_private:false}},
{term:{sub_location:can}},
{term:{sub_location:
{term:{sub_location:chain}},
{term:{sub_location:these}},
{term:{sub_location:queries }}],
fquery:
{query:
{match:
{by_line:JohnKealy / BFAnyc}}}}}}

搜索结果中出现的记录如下(注意sub_location与我的子位置查询不符)

  {event:{id:1,
sub_location:纽约,
state
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 ,
visible:nil,
zip_created_at:nil,
zip_processing:nil,
position:0,
pdf ,
cover_photo_id:nil,
created_at:2015-07-16T15:53:26.000Z,
updated_at:2015-07-16T15:53:26.000 Z,
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 字段。这意味着价值约翰·凯亚/ BFA.com被分别索引为john,kealy,bfa和com。现在术语查询适用于未分析的字段。正在搜索完整的术语John Kealy / BFA.com,这当然不在反向索引中。您需要使用匹配查询,而不是术语查询如下:

  {
query:{
match:{
by_line:John Kealy / BFA.com
}
}
}

如果你想要这样做的过滤器,您可以使用 fquery 如下:

  {
filter:{
fquery:{
query:{
match:{
by_line:John Kealy / BFA.com
}
},
_cache:true
}
}
}


I run a photography website. Photographers put their "by_line" as "Some name/ourwebsite.com".

I have a DSL i'm using for most of my queries, stringing together "filter" queries using "terms" which works great for 90% of cases, however in this case i'm getting zero results back with a query like this (notice how i tried to escape the forward slash):

body: 
  {query:
    {filtered: 
      {filter: 
        {and: 
          [{term: {is_visible: true}}, 
          {term: {"event.by_line": "john\\/my_website.com"}}]
         }
      }
    }
  }

I'm using elasticsearch version 1.5.2

When i look at mappings at this URL

http://127.0.0.1:9200/development_photos/_mapping?pretty=1

this is an example record

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

and mappings

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

As per bittusarkar's suggestion i updated my query to use fquery. Now I have two more questions. How can I chain fquery queries?

This will result in a elastic search failure

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

This results in the error "query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?"

Furthermore, when I try to mix fquery with my old way of just chaining "and" filters, it returns results that it shouldn't.. It appears it's ignoring the "and" queries. For example:

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

Records like this come up in the search results: (notice how "sub_location doesn't match my sub_location query)

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

解决方案

From your mapping it looks like you are using the default analyzer for by_line field. What this means is that the value "John Kealy/BFA.com" is indexed as the following terms separately - "john", "kealy", "bfa" and "com". Now term query works for non-analyzed fields. It is searching for the complete term "John Kealy/BFA.com" which of course is not present in the inverted index. You need to use match query instead of term query here as below:

{
   "query": {
      "match": {
         "by_line": "John Kealy/BFA.com"
      }
   }
}

If you want this to be part of a filter, you can use fquery as below:

{
   "filter": {
      "fquery": {
         "query": {
            "match": {
               "by_line": "John Kealy/BFA.com"
            }
         },
         "_cache": true
      }
   }
}

这篇关于在弹性搜索中逃避斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆