# Help with writing a search query

**URL:** <https://discuss.elastic.co/t/help-with-writing-a-search-query/153808>\
**Category:** Elasticsearch\
**Created:** [October 24, 2018, 1:16pm UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808 "2018-10-24T13:16:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProxyMasterMerger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/proxymastermerger/32/42288_2.png) [@ProxyMasterMerger](https://discuss.elastic.co/u/ProxyMasterMerger)\
**Post date:** [October 24, 2018, 1:16pm UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808/1 "2018-10-24T13:16:03Z")

</div>

Hello all, nooby here. I'm trying to write a search query but I'm having some problems.

I have an index called Categories. It has the following fields: title, tags (which is just a big string with keywords) and total, a number which represents the total number of items I have for this category.

Given search query T, I want to fetch 10 results where title/tags are T, or begin with T, or contain T. I want to sort them based on score, and then based on total. I also want to disable term frequency consideration into the score. That's the gist of it.

```auto
    {
      "index": "categories",
      "body": {
        "size": 10,
        "query": {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "query_string": {
                  "default_field": "_all",
                  "query": "T"
                }
              },
              {
                "query_string": {
                  "default_field": "_all",
                  "query": "T*"
                }
              },
              {
                "query_string": {
                  "default_field": "_all",
                  "query": "*T*"
                }
              }
            ],
            "must": [
              {
                "range": {
                  "total": {
                    "gt": 0
                  }
                }
              }
            ]
          }
        },
        "sort": {
          "_score": {
            "order": "desc"
          },
          "total": {
            "order": "desc"
          }
        }
      }
    }

```

This query works, however I'm unable to impose the no-term frequency rule. I've tried writing constant\_score to encapsulate each of those query\_strings or the bool, but I only got exceptions.

Any ideas? Also, just making sure but am I writing my query correctly, or should I write it in a different fashion? Thanks 🙂

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 24, 2018, 2:04pm UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808/2 "2018-10-24T14:04:38Z")

</div>

Please wrap your code snippets in ``` so discuss doesn't eat the formatting.

> [@ProxyMasterMerger](#):
>
> "query\_string": {

Using `query_string` for user generated queries can be a trap. When `query_string` doesn't understand some syntax it just sends back an error. I ended up working myself into a local minimal where I had a doen regexes that I ran on use queries to "prepare" them for `query_string`. Usually you are better of just using `match` and a `match_prefix` query.

`constant_score` should get the job done if you just want each of the two arms of the bool query to be worth a particular amount. Wrap each of the `should` queries in `constant_score`.

---

<div class="post-metadata">

**Author:** ![ProxyMasterMerger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/proxymastermerger/32/42288_2.png) [@ProxyMasterMerger](https://discuss.elastic.co/u/ProxyMasterMerger)\
**Post date:** [October 24, 2018, 2:25pm UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808/3 "2018-10-24T14:25:00Z")

</div>

Alright, sorry about that. I tried to format the JSON before but I guess I didn't do it right.  
Anyway, I'm getting the following exception when attempting your solution:  
[bool] query does not support [constant\_score]

I should've clarified this before but I run my queries via PHP code in a Laravel-based environment. I use elasticsearch version 6.0.1 and ruflin/elastica version 6. Not sure if this will help, but yeah... Any ideas?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 24, 2018, 3:23pm UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808/4 "2018-10-24T15:23:27Z")

</div>

You should be able to stick a `constant_score` query in the same place as have `query_string` now. You'd put the `query_string` inside the `filter` object in the `constant_score` one. That _should_ work.

> [@ProxyMasterMerger](#):
>
> I should've clarified this before but I run my queries via PHP code in a Laravel-based environment. I use elasticsearch version 6.0.1 and ruflin/elastica version 6. Not sure if this will help, but yeah... Any ideas?

I used reflin's elastica for years but I haven't worked on that project in a few years. So, if it does matter, I won't know. But it _shouldn't_ matter. Like, from the philosophical standpoint.

---

<div class="post-metadata">

**Author:** ![ProxyMasterMerger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/proxymastermerger/32/42288_2.png) [@ProxyMasterMerger](https://discuss.elastic.co/u/ProxyMasterMerger)\
**Post date:** [October 25, 2018, 6:19am UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808/5 "2018-10-25T06:19:10Z")

</div>

Great! I forgot to write filter block inside the constant\_score query, that was my problem.  
Seems to be working as expected now, thanks for the help! 😁

---

<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:** [November 22, 2018, 6:19am UTC](https://discuss.elastic.co/t/help-with-writing-a-search-query/153808/6 "2018-11-22T06:19:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
