I think the idea that Radu outlined might work. But I would propose a few
changes to the implementation. First of all, retrieving and parsing source
for every single result can be very expensive especially if records have
large sources. So, it might be better to replace _source lookup with doc or
_field lookup.
The expression "_source.foo is Integer" would work only if "foo" was an
integer in the source. In other words, if it was indexed as {"foo": 123} it
would work, but if it was indexed as {"foo": "123"} it wouldn't. In order
to handle the latter case, we need to actually parse the string.
To answer, Nick's question, we can simplify handling of non-numeric cases,
by switching from custom_score query to sort script .
So, if we combine all these suggestions, we will get something like this:
{
"query" : {
"match_all": {}
},
"sort": {
"_script":{
"script" : "s = doc['''foo'''].value; n =
org.elasticsearch.common.primitives.Ints.tryParse(s); if (n != null) {
String.format("%010d",n)} else { s }",
"type" : "string"
}
}
}'
That still might be too slow for the large result list. So, an ideal
solution here would be to pad integer values with 0 so all integers have
the same size during indexing. In other words, do the same thing that the
script above is doing but during indexing.
Here is a complete example if somebody wants to play with
it: Sorting mixed integer/string field · GitHub
On Monday, October 29, 2012 8:59:26 AM UTC-4, Nick Hoffman wrote:
Hi Radu. That's an interesting idea. How would you score fields that
aren't numeric, though?On Monday, 29 October 2012 05:49:37 UTC-4, Radu Gheorghe wrote:
Hello,
You can use the _source field, if you got it stored, in a script
within a custom_score query. For example, assuming "foo" is the name
of our field:curl -XPOST localhost:9200/test/test/_search?pretty=true -d '{
"query": {
"custom_score": {
"query": {
"match_all": {}
},
"script": "if (_source.foo is Integer) { _source.foo }
else { _score/5 }"
}
}
}'This will get your numeric values sorted as numbers. You'd need to
fill out the "else" part of the script with whatever values you want
to give to your non-numeric fields.Best regards,
Raduhttp://sematext.com/ -- Elasticsearch -- Solr -- Lucene
On Sun, Oct 28, 2012 at 9:10 PM, Nick Hoffman ni...@deadorange.com
wrote:Hah, I asked the exact same question:
Redirecting to Google GroupsOn Sunday, 28 October 2012 14:45:25 UTC-4, James Hu wrote:
I have a field that is string type. Sometimes it will contain integer
values such as 1, 2, 10, 20. Currently how it sorts those is 1, 10, 2,
How do I go aboutsorting that numerically so that it's 1, 2, 10, 20?
--
--