elasticsearch 按字符串长度的弹性搜索顺序
我正在通过NEST c#使用ElasticSearch。我有一大堆关于人的信息elasticsearch 按字符串长度的弹性搜索顺序,elasticsearch,nest,sense,elasticsearch,Nest,Sense,我正在通过NEST c#使用ElasticSearch。我有一大堆关于人的信息 { firstName: 'Frank', lastName: 'Jones', City: 'New York' } 我想能够过滤和排序这个项目列表的姓氏以及顺序的长度,使人谁只有5个字符在他们的名字将在结果集的开始,然后人与10个字符 所以我想用一些伪代码 list.wildcard(“j*”).sort(m=>lastName.length) 我是ElasticSearch的新手,因此任何示
{
firstName: 'Frank',
lastName: 'Jones',
City: 'New York'
}
我想能够过滤和排序这个项目列表的姓氏以及顺序的长度,使人谁只有5个字符在他们的名字将在结果集的开始,然后人与10个字符
所以我想用一些伪代码
list.wildcard(“j*”).sort(m=>lastName.length)
我是ElasticSearch的新手,因此任何示例都会非常有用。您可以使用进行排序 作为一个玩具示例,我用几个文档建立了一个简单的索引:
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"}
然后我可以按如下方式订购搜索结果:
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
]
}
]
}
}
要按长度过滤,我可以使用类似的方法:
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
]
}
]
}
}
以下是我使用的代码:
p.S.:如果姓氏中有任何一个包含空格,您可能希望在该字段上使用。假设他经常在许多文档上执行此搜索,是否值得为长度编制索引?感谢您的反馈@罗宾:在大多数情况下,我的数据不会改变,所以索引长度对我来说是有益的。如果你有任何参考资料,你可以告诉我那将是伟大的。点击进入按钮太快。我在使用Sense时也遇到了一个问题,它抛出了一个异常,声明groovy被禁用。将groovy指定为语言,并更改删除_脚本上的下划线,这会让我遇到更多错误。我知道这很琐碎,对运行示例的环境有什么建议吗?我尝试了你发送到qBox的链接,但它把我带到了一个空编辑器,也许你忘了保存代码?我在ES1.3.4上运行了这个。在1.4+版本中,您必须使CORS能够使用Sense应用程序以及动态脚本。不过,索引长度可能是更好的生产解决方案。我认为,您必须计算代码中的长度。不过,我不知道为什么编辑器对您来说是空的;它对我有用。你用的是什么浏览器?您是否在控制台中看到任何JavaScript错误?你能帮我把它们贴在什么地方吗?谢谢斯隆,我可能在防火墙后面,我今晚晚些时候再试试