आप script-based sorting के साथ सॉर्टिंग कर सकते हैं। ,
POST /test_index/_search
{
"query": {
"match_all": {}
},
"sort": {
"_script": {
"script": "doc['name'].value.length()",
"type": "number",
"order": "asc"
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": null,
"_source": {
"name": "Bob"
},
"sort": [
3
]
},
{
"_index": "test_index",
"_type": "doc",
"_id": "4",
"_score": null,
"_source": {
"name": "Jose"
},
"sort": [
4
]
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": null,
"_source": {
"name": "Jeff"
},
"sort": [
4
]
},
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": null,
"_source": {
"name": "Darlene"
},
"sort": [
7
]
}
]
}
}
लंबाई के आधार पर फ़िल्टर करने के लिए मैं कर सकते हैं:
PUT /test_index
POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"Bob"}
{"index":{"_id":2}}
{"name":"Jeff"}
{"index":{"_id":3}}
{"name":"Darlene"}
{"index":{"_id":4}}
{"name":"Jose"}
तो मैं इस तरह खोज परिणामों आदेश कर सकते हैं:
एक खिलौना उदाहरण के रूप में, मैं कुछ दस्तावेजों के साथ एक छोटी सी सूचकांक की स्थापना एक समान तरीके से एक script filter का उपयोग करें:
POST /test_index/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"script": {
"script": "doc['name'].value.length() > 3",
"params": {}
}
}
}
},
"sort": {
"_script": {
"script": "doc['name'].value.length()",
"type": "number",
"order": "asc"
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "4",
"_score": null,
"_source": {
"name": "Jose"
},
"sort": [
4
]
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": null,
"_source": {
"name": "Jeff"
},
"sort": [
4
]
},
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": null,
"_source": {
"name": "Darlene"
},
"sort": [
7
]
}
]
}
}
यहाँ कोड मैं प्रयोग किया जाता है:
http://sense.qbox.io/gist/22fef6dc5453eaaae3be5fb7609663cc77c43dab
पी.एस .: तो अंतिम नाम के किसी भी रिक्त स्थान में शामिल होंगे, तो आप उस मैदान पर "index": "not_analyzed"
उपयोग करने के लिए चाहते हो सकता है।
मान लें कि वह अक्सर इस खोज को कई दस्तावेजों पर करता है, क्या यह केवल लंबाई को अनुक्रमणित करने योग्य होगा? – Robin
हाँ, निश्चित रूप से, अगर यह एक विकल्प है। अच्छी बात। –
इस तरह के महान प्रतिक्रिया के लिए धन्यवाद। @ रोबिन: अधिकांश भाग के लिए मेरा डेटा नहीं बदलेगा, इसलिए लंबाई को अनुक्रमणित करना मुझे लगता है कि यह फायदेमंद होगा। यदि आपके पास कोई संदर्भ है तो आप मुझे इंगित कर सकते हैं कि यह बहुत अच्छा होगा। – mvcNewbie