Chrift
Chrift

Reputation: 346

Why isn't this elasticsearch query matching a string exactly?

I have an elasticsearch query that isn't matching a document that it should.

Here is the query:

POST _search
{
   "query": {
      "filtered": {
         "filter": {
            "bool": {
               "must": {
                  "term": {
                     "signupCompany": "NewBay Media"
                  }
               }
            }
         }
      }
   }
}

However, if I change the term to:

"signupCompanyID": 643

Then it finds the correct document. Here is my mapping:

"properties": {
      "body": {
        "type": "string"
      },
      "datetimeIndexed": {
        "type": "date",
        "format": "epoch_millis||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
      },
      "datetimeReceived": {
        "type": "date",
        "format": "epoch_millis||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
      },
      "duplicateOfEmailID": {
        "type": "long"
      },
      "enabled": {
        "type": "boolean"
      },
      "htmlBodyPath": {
        "type": "string"
      },
      "plainBodyPath": {
        "type": "string"
      },
      "secondsAgoReceived": {
        "type": "long"
      },
      "senderName": {
        "type": "string",
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      },
      "signupCompany": {
        "type": "string"
      },
      "signupCompanyEnabled": {
        "type": "boolean"
      },
      "signupCompanyID": {
        "type": "long"
      },
      "signupCompanyInitial": {
        "type": "string"
      },
      "subject": {
        "type": "string"
      }
    }

I have some other queries that are already working and in production, using the datetimeReceived field and enabled field, but haven't been able to get this particular string match to work.

It won't work with any signupCompany, not just this one.

Upvotes: 0

Views: 80

Answers (1)

court3nay
court3nay

Reputation: 2365

You need to set the mapping type for signupCompany to not_analyzed so that it doesn't try to match a partial string, only the exact value. - docs: https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-intro.html

"signupCompany": {
  "type":     "string",
  "index":    "not_analyzed"
}

Upvotes: 1

Related Questions