Sort same scored documents by field


(Matt Arkin) #1

I have a bunch of documents across several indexes that might have a field
(tags), that all have timestamps, and that all have the same id.
I want to search them so that if they have tags (so if tags exists), that
they have a higher score, and then sort all the documents by timestamp.

I noticed if I add a sort by timestamp descending to my query though, that
the order is based purely on timestamp and not on whether the tags exist.

So for instance I have this query:

   query: {
        custom_filters_score: {
          query: {
            bool: {
              must: [
                {
                  text: {
                    _id: title
                  }
                }
              ],
              must_not: [],
              should: []
            }
          },
          filters: [
            {
              filter: {
                exists: {
                  field: "tags"
                }
              },
              boost: "2"
            }
          ],
          score_mode: "first"
        }
      }
}

this returns a bunch of documents with either score 1 (if it doesn't have tags) or score 2 (if it does).

So can I sort them by score and then by timestamp?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Adrien Grand) #2

Hi,

You can ask Elasticsearch to sort by _score and then timestamp, for example:

"sort" : [
"_score",
{ "timestamp" : {"order" : "desc" } }
]

On Fri, Oct 25, 2013 at 8:32 PM, Matt Arkin arkin@endlessm.com wrote:

I have a bunch of documents across several indexes that might have a field
(tags), that all have timestamps, and that all have the same id.
I want to search them so that if they have tags (so if tags exists), that
they have a higher score, and then sort all the documents by timestamp.

I noticed if I add a sort by timestamp descending to my query though, that
the order is based purely on timestamp and not on whether the tags exist.

So for instance I have this query:

   query: {
        custom_filters_score: {
          query: {
            bool: {
              must: [
                {
                  text: {
                    _id: title
                  }
                }
              ],
              must_not: [],
              should: []
            }
          },
          filters: [
            {
              filter: {
                exists: {
                  field: "tags"
                }
              },
              boost: "2"
            }
          ],
          score_mode: "first"
        }
      }
}

this returns a bunch of documents with either score 1 (if it doesn't have tags) or score 2 (if it does).

So can I sort them by score and then by timestamp?

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
Adrien Grand

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Matt Arkin) #3

Well that was easy. Thanks :slight_smile:

On Sun, Oct 27, 2013 at 1:55 PM, Adrien Grand <
adrien.grand@elasticsearch.com> wrote:

Hi,

You can ask Elasticsearch to sort by _score and then timestamp, for
example:

"sort" : [

    "_score",
    { "timestamp" : {"order" : "desc" } }

]

On Fri, Oct 25, 2013 at 8:32 PM, Matt Arkin arkin@endlessm.com wrote:

I have a bunch of documents across several indexes that might have a
field (tags), that all have timestamps, and that all have the same id.
I want to search them so that if they have tags (so if tags exists), that
they have a higher score, and then sort all the documents by timestamp.

I noticed if I add a sort by timestamp descending to my query though,
that the order is based purely on timestamp and not on whether the tags
exist.

So for instance I have this query:

   query: {
        custom_filters_score: {
          query: {
            bool: {
              must: [
                {
                  text: {
                    _id: title
                  }
                }
              ],
              must_not: [],
              should: []
            }
          },
          filters: [
            {
              filter: {
                exists: {
                  field: "tags"
                }
              },
              boost: "2"
            }
          ],
          score_mode: "first"
        }
      }
}

this returns a bunch of documents with either score 1 (if it doesn't have tags) or score 2 (if it does).

So can I sort them by score and then by timestamp?

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
Adrien Grand

--
You received this message because you are subscribed to a topic in the
Google Groups "elasticsearch" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/elasticsearch/PZWiwbBEHL0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #4