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

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.

Welcome Benjamin!

Yes. You can do a 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.

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:

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:

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

HTH