# Can I sort a sematic query based on a field containing a value or not?

**URL:** https://discuss.elastic.co/t/can-i-sort-a-sematic-query-based-on-a-field-containing-a-value-or-not/383906
**Category:** Elasticsearch
**Created:** [December 6, 2025, 10:23am UTC](https://discuss.elastic.co/t/can-i-sort-a-sematic-query-based-on-a-field-containing-a-value-or-not/383906 "2025-12-06T10:23:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bwagner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bwagner/32/144235_2.png) [@bwagner](https://discuss.elastic.co/u/bwagner)
#### Post date: [December 6, 2025, 10:23am UTC](https://discuss.elastic.co/t/can-i-sort-a-sematic-query-based-on-a-field-containing-a-value-or-not/383906/1 "2025-12-06T10:23:05Z")

</div>

I am new to elastic an Elasticsearch and I guess this is a fairly simple, beginner type quesation.

I have an index that contains information from websites and pdf documents. The index is used in a chat bot on an a website.

In the results the bot should use the information from both sources but should prefer the content from the website so that it returns the weblink as a source.  
The weblink is stored in a separate field and is only field by the websites, for pdf documents the field is empty.

Can I order the query based on this field. More specific I want documents where the link field is filled to be the top results.

Is that possible?

I was looking into query rules but since I don’t know the ids prior to the query, I think they are not the right choice.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 6, 2025, 11:37am UTC](https://discuss.elastic.co/t/can-i-sort-a-sematic-query-based-on-a-field-containing-a-value-or-not/383906/2 "2025-12-06T11:37:19Z")

</div>

Welcome Benjamin!

Yes. You can do a [bool query](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-bool-query), with a should array.  
One of the inner should query will be your match query (what the user is looking for)  
The other one will be an [exists query](https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-exists-query).

That way, when the exists query matches (ie, the field is there), the score added by this query will increase the global score.

It will look a bit like this:

```auto
POST _search
{
  "query": {
    "bool" : {
      "should" : [
        { "exists" : { "field" : "url" } },
        { "match" : { "content" : "foo bar baz" } }
      ]
    }
  }
}

```

And of course you can replace the match query by a semantic query:

```auto
    "semantic": {
      "field": "content.semantic",
      "query": "foo bar baz"
    }

```

HTH
