pferrel
pferrel

Reputation: 5702

How to force Elasticsearch "terms" query to be not_analyzed

I want to make exact matches ids in a doc field. I have mapped the fields to index them not_analyzed but it seems like in the query each term is tokenizde or at least lowercased. How do I make the query also not_analyzed? Using ES 1.4.4, 1.5.1, and 2.0.0

Here is a doc:

 {
    "_index": "index_1446662629384",
    "_type": "docs",
    "_id": "Cat-129700",
    "_score": 1,
    "_source": {
       "similarids": [
          "Cat-129695",
          "Cat-129699",
          "Cat-129696"
       ],
       "id": "Cat-129700"
    }
 }

Here is a query:

{
    "size": 10,
    "query": {
        "bool": {
            "should": [{
                "terms": {
                    "similarids": ["Cat-129695","Cat-129699","Cat-129696"]
                }
            }]
        }
    }
}

The query above does not work. If I remove caps and dashes from the doc ids it works. I can't do that for many reasons. Is there a way to make the similarids not_analyzed like the doc fields?

Upvotes: 2

Views: 510

Answers (1)

Sloan Ahrens
Sloan Ahrens

Reputation: 8718

If I'm understanding you correctly, all you need to do is set "index":"not_analyzed" on the "similarids" in your mapping. If you have that setting correct already, then there is something else going on that isn't apparent from what you posted (the "terms" query doesn't do any analysis on your search terms). You may want to check your mapping to make sure it is set up the way you think.

To test it, I set up a simple index like this:

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "id": {
               "type": "string",
               "index": "not_analyzed"
            },
            "similarids": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

Then added your document:

PUT /test_index/doc/1
{
   "similarids": [
      "Cat-129695",
      "Cat-129699",
      "Cat-129696"
   ],
   "id": "Cat-129700"
}

And your query works just fine.

POST /test_index/_search
{
   "size": 10,
   "query": {
      "bool": {
         "should": [
            {
               "terms": {
                  "similarids": [
                     "Cat-129695",
                     "Cat-129699",
                     "Cat-129696"
                  ]
               }
            }
         ]
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.53148466,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.53148466,
            "_source": {
               "similarids": [
                  "Cat-129695",
                  "Cat-129699",
                  "Cat-129696"
               ],
               "id": "Cat-129700"
            }
         }
      ]
   }
}

I used ES 2.0 here, but it shouldn't matter which version you use. Here is the code I used to test:

http://sense.qbox.io/gist/562ccda28dfaed2717b43739696b88ea861ad690

Upvotes: 1

Related Questions