# Searching with \_id throws NullPointerException

**URL:** <https://discuss.elastic.co/t/searching-with--id-throws-nullpointerexception/3592>\
**Category:** Elasticsearch\
**Created:** [November 22, 2010, 1:58pm UTC](https://discuss.elastic.co/t/searching-with--id-throws-nullpointerexception/3592 "2010-11-22T13:58:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sezgin\_kucukkaraasla](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@sezgin\_kucukkaraasla](https://discuss.elastic.co/u/sezgin_kucukkaraasla)\
**Post date:** [November 22, 2010, 1:58pm UTC](https://discuss.elastic.co/t/searching-with--id-throws-nullpointerexception/3592/1 "2010-11-22T13:58:56Z")

</div>

Hi,  
As you know, when I leave id generation to elasticsearch it provides some  
unique strings to my entries like "FRS94Gd3SL-DCjYE9ld-7Q", "  
-k5EX5ENRMSOQZSYj-xXRw", etc. Searching with id with the following query is  
ok:

\_id:FRS94Gd3SL-DCjYE9ld-7Q

But when an id starts with a minus sign elasticsearch throws a null pointer  
exception:

\_id:-k5EX5ENRMSOQZSYj-xXRw

Here is the exception:

{

- error: "SearchPhaseExecutionException[Failed to execute phase [query],  
total failure; shardFailures {null:  
ArrayIndexOutOfBoundsException[57]}{null:  
ArrayIndexOutOfBoundsException[57]}{null:  
ArrayIndexOutOfBoundsException[57]}{null:  
ArrayIndexOutOfBoundsException[57]}{null:  
ArrayIndexOutOfBoundsException[57]}]"

}

Thanks in advance,

Sezgin Kucukkaraaslan  
[www.ifountain.com](http://www.ifountain.com)

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [November 22, 2010, 4:12pm UTC](https://discuss.elastic.co/t/searching-with--id-throws-nullpointerexception/3592/2 "2010-11-22T16:12:12Z")

</div>

Hiya

> But when an id starts with a minus sign elasticsearch throws a null  
> pointer exception:
> 
> \_id:-k5EX5ENRMSOQZSYj-xXRw

The exception itself should be caught and transformed into a better  
error message (please open an issue for this:  
[Issues · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues) )

However, the reason for the error is this: a leading '-' is a special  
character in the lucene query parser  
[http://lucene.apache.org/java/3\_0\_0/queryparsersyntax.html](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)

It means "exclude results containing this term", so effectively a search  
for '\_id:-k5EX5ENRMSOQZSYj-xXRw' means:

```
"give me not k5EX5ENRMSOQZSYj-xXRw"

```

but without specifying any other clause that SHOULD match, so ES doesn't  
know what to exclude k5EX5ENRMSOQZSYj-xXRw from.

You can get around that by escaping the '-', eg:

```
    curl 'http://localhost:9200/_search?q=_id:
    \-k5EX5ENRMSOQZSYj-xXRw'

```

Alternatively, you could just request that doc:

```
    curl 'http://localhost:9200/myindex/mytype/-k5EX5ENRMSOQZSYj-xXRw' 

```

Or, you could use a term query:

```
    curl -XGET 'http://localhost:9200/_all/_search' -d '
    {
       "query" : {
          "term" : {
             "_id" : "-k5EX5ENRMSOQZSYj-xXRw"
          }
       }
    }
    '

```

clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 22, 2010, 10:55pm UTC](https://discuss.elastic.co/t/searching-with--id-throws-nullpointerexception/3592/3 "2010-11-22T22:55:36Z")

</div>

Hey,

Yea, that failure is ugly..., I fixed it to have the proper Lucene query  
parsing failure (which is waaay too detailed 😉 ).

The id generation in elasticsearch has changed, it is still a 128 UUID  
generation, but it is encoded using baset64 (modified to be URL friendly).  
Was thinking long on which encoding should be used instead of how it was  
represented before (which is waay too verbose and too long), and thought  
that base64 (modified to be URL friendly) is the best choice. Is more than  
open to other ideas if someone has them (for UUID based representation, I  
know that generating unique ids can be done in other ways).

-shay.banon

On Mon, Nov 22, 2010 at 6:12 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> Hiya
> 
> > But when an id starts with a minus sign elasticsearch throws a null  
> > pointer exception:
> > 
> > \_id:-k5EX5ENRMSOQZSYj-xXRw
> 
> The exception itself should be caught and transformed into a better  
> error message (please open an issue for this:  
> [Issues · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues) )
> 
> However, the reason for the error is this: a leading '-' is a special  
> character in the lucene query parser  
> [Apache Lucene - Query Parser Syntax](http://lucene.apache.org/java/3_0_0/queryparsersyntax.html)
> 
> It means "exclude results containing this term", so effectively a search  
> for '\_id:-k5EX5ENRMSOQZSYj-xXRw' means:
> 
> "give me not k5EX5ENRMSOQZSYj-xXRw"
> 
> but without specifying any other clause that SHOULD match, so ES doesn't  
> know what to exclude k5EX5ENRMSOQZSYj-xXRw from.
> 
> You can get around that by escaping the '-', eg:
> 
> ```
> curl 'http://localhost:9200/_search?q=_id:
> \-k5EX5ENRMSOQZSYj-xXRw'
> 
> ```
> 
> Alternatively, you could just request that doc:
> 
> ```
> curl 'http://localhost:9200/myindex/mytype/-k5EX5ENRMSOQZSYj-xXRw'
> 
> ```
> 
> Or, you could use a term query:
> 
> ```
> curl -XGET 'http://localhost:9200/_all/_search' -d '
> {
> "query" : {
> "term" : {
> "_id" : "-k5EX5ENRMSOQZSYj-xXRw"
> }
> }
> }
> '
> 
> ```
> 
> clint

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:16am UTC](https://discuss.elastic.co/t/searching-with--id-throws-nullpointerexception/3592/4 "2017-07-06T04:16:10Z")

</div>


