2
votes

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}}
1
The question is, what do you have in your index? Do you have indexed that slash? What analyzer have you used at indexing time for by_line field?Andrei Stefan
Andrei is correct, as usual. The data indexed will depend on the tokenizer/TokenFilters used in your analyzer. Please check your mapping (or post it) for further help.Daniel Hoffmann-Mitscherling

1 Answers

4
votes

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