# Most efficient way to query without a score

**URL:** https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457
**Category:** Elasticsearch
**Created:** [August 8, 2016, 9:07am UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457 "2016-08-08T09:07:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)
#### Post date: [August 8, 2016, 9:07am UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/1 "2016-08-08T09:07:28Z")

</div>

Hey,

I am trying to figure out what is the most efficient way to query elasticsearch without scoring. (I assume that scoring add more overhead, and no scoring make it faster).

I need to be able to say filter all that is not. (must\_not)

So If I want to build a query that will result let's say with the documents that have the string "some\_name" in field companyName, the creation date is after "2016-07-20" and must not have "foo" in companyName:

```
{
   "query": {
      "bool": {
         "filter": { 
            "bool": {
               "must": [
                  { "regexp": { "companyName": ".*some_name.*"} },
                  { "range": { "creationDate": { "from": "2016-07-20" }}}
               ], 
               "must_not":[
                     {"term": { "companyName" : "foo"}}
               ]
            }
         }
      }
   },
   "size": 150
}

```

Is this the most efficient way?  
Do I have to use a bool query inside a filter to have the must not functionality? using constant\_score is better?

I am a little confused..  
Thanks!!

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [August 8, 2016, 9:15am UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/2 "2016-08-08T09:15:34Z")

</div>

You don't need to wrap `bool/must_not` inside `bool/filter` as `bool/must_not` is already executed in a filter context. You can simply do it like this:

```
{
   "query": {
      "bool": {
         "filter": [
              { "regexp": { "companyName": ".*some_name.*"} },
              { "range": { "creationDate": { "from": "2016-07-20" }}}
          ], 
          "must_not":[
              {"term": { "companyName" : "foo"}}
          ]
      }
   },
   "size": 150
}

```

It is worth noting, though, that your regexp filter will not be very efficient. If you want to improve the speed of your query, you'll probably want to use an ngram token filter and then use a `term` query on `companyName`, that will be much more efficient.

---

<div class="post-metadata">

### Author: ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)
#### Post date: [August 8, 2016, 9:32am UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/3 "2016-08-08T09:32:47Z")

</div>

Thanks!

---

<div class="post-metadata">

### Author: ![taras](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/taras/32/507_2.png) [@taras](https://discuss.elastic.co/u/taras)
#### Post date: [August 8, 2016, 2:36pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/4 "2016-08-08T14:36:38Z")

</div>

You also want to use _\_doc_ sort order on the entire query: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html)

---

<div class="post-metadata">

### Author: ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)
#### Post date: [August 10, 2016, 11:56am UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/5 "2016-08-10T11:56:53Z")

</div>

Why would I want it? @taras

---

<div class="post-metadata">

### Author: ![taras](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/taras/32/507_2.png) [@taras](https://discuss.elastic.co/u/taras)
#### Post date: [August 12, 2016, 8:57pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/6 "2016-08-12T20:57:26Z")

</div>

If you're concerned about cost of scoring and sorting, then you can tell elasticsearch to return documents in their natural appearance order `_doc`. It is simply a more efficient way to order documents. Do note that you will notice any real impact only if you're matching a large number of documents.

As Val mentioned, your main performance hit would come from `regexp` query. In addition to replacing it with `ngram` your `companyName` field could be possibly tokenized based on some boundary rule, or even regex. That would be more efficient than regex query and in some instances better than ngram approach since it would generate fewer terms per document.

---

<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 5, 2017, 10:27pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-query-without-a-score/57457/7 "2017-07-05T22:27:57Z")

</div>


