Can't seem to get results for boolean fields


(Trezy Peebles) #1

Hey guys, I'm having issues trying to search against boolean fields and I can't seem to figure out what the deal is. I've been googling and banging my head against the problem for days now. I've got a Node application and I'm using Mongoosastic to make create indexes from my MongoDB. It's also creating mappings based on my Mongoose models.

Here's the whole project: https://github.com/FuelRats/api.fuelrats.com

Here's the particular model I'm working on: https://github.com/FuelRats/api.fuelrats.com/blob/elasticsearch/api/models/rat.js

I've got the archive field indexed as a boolean and regardless of how I run the search I can't seem to get any results. Here's what my query looks like at the moment:

{
  query: {
    filtered: {
      query: {
        must: [
          match: {
            CMDRname: {
              term: 'Trezy',
              fuzziness: 'auto'
            }
          }
        ]
      },
      filter: {
        term: {
          archive: true
        }
      }
    }
  }
}

(Daniel Mitterdorfer) #2

Hi,

I am not sure where the exact problem is, maybe it is related to the mapping that is created for you. You can check the mapping with:

GET /fuelrats/_mapping

(where "fuelrats" is the index name)

Here is a pure Elasticsearch example that you can paste directly into Sense and play around there:

We first create the index. I have not included all properties that you use though:

PUT /fuelrats
{
   "mappings": {
      "rat": {
         "properties": {
            "archive": {
               "type": "boolean"
            },
            "CMDRname": {
                type: "string"
            }
         }
      }
   }
}

Then we index a few documents:

POST /_bulk
{"index":{"_index":"fuelrats","_type":"rat"}}
{"CMDRname":"Crazy","archive":false}
{"index":{"_index":"fuelrats","_type":"rat"}}
{"CMDRname":"Trezy","archive":true}
{"index":{"_index":"fuelrats","_type":"rat"}}
{"CMDRname":"Tracy","archive":true}
{"index":{"_index":"fuelrats","_type":"rat"}}
{"CMDRname":"Tracey","archive":true}
{"index":{"_index":"fuelrats","_type":"rat"}}
{"CMDRname":"Trezi","archive":true}
{"index":{"_index":"fuelrats","_type":"rat"}}
{"CMDRname":"Trazy","archive":true}

And fuzzy-search for them:

GET /_search
{
  "query": {
    "filtered": {
      "query": {
          "fuzzy": {
            "CMDRname": {
              "term": "Trezy",
              "fuzziness": "auto"
            }
          }
      },
      "filter": {
        "term": {
          "archive": true
        }
      }
    }
  }
}

This returns one hit; the document with CMDRname "Trezy". Note that fuzzy queries do not run through the analyzer. This is how Elasticsearch sees the "CMDRname" property of a "rat" document:

GET /fuelrats/_analyze
{
    "field": "CMDRname",
    "text": "Trezi"
}

this produces:

{
   "tokens": [
      {
         "token": "trezi",
         "start_offset": 0,
         "end_offset": 5,
         "type": "<ALPHANUM>",
         "position": 0
      }
   ]
}

So, the standard analyzer that is used for analyzing the text produces "trezi". But a fuzzy query will not run through this analysis stage and will internally try to fuzzy-match "Trezi" against "trezi". As the field "CMDRname" seems to be some kind of username you probably want to set index to "not_analyzed" anyway (see docs on index mapping options).

By the way, the filtered query is deprecated starting from Elasticsearch 2.0 (see also https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html). You should use a bool query instead:

GET /_search
{
  "query": {
    "bool": {
      "must": {
          "fuzzy": {
            "CMDRname": {
              "term": "Trezy",
              "fuzziness": "auto"
            }
          }
      },
      "filter": {
        "term": {
          "archive": true
        }
      }
    }
  }
}

I hope that solves your problem.

Daniel


(system) #3