gpt4 book ai didi

Elasticsearch - 查找至少有 x 个元素与给定数组相同的记录

转载 作者:行者123 更新时间:2023-11-29 02:54:42 25 4
gpt4 key购买 nike

我有这样的映射:

"properties": {
"id": {"type": "long", "index": "not_analyzed"},
"name": {"type": "string", "index": "not_analyzed"},
"skills": {"type": "string", "index": "not_analyzed"}
}

我想使用给定的映射将学生的个人资料存储在 elasticsearch 中。 skills 是他们在个人资料中指定的计算机技能列表(python、javascript 等)。

鉴于 ['html', 'css', 'sass', 'javascript', 'django', 'bootstrap', 'angularjs', 'backbone'],我想要找到至少具有此技能集中 3 种技能的所有配置文件。我对了解他们与我们想要的列表有哪些共同技能不感兴趣,只对计数感兴趣。有没有办法在 elasticsearch 中做到这一点?

最佳答案

可能有更好的方法我没有想到,但您可以使用 script filter 来实现.

我设置了一个简化版本的索引,其中包含一些文档:

PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"skills": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"skills":["html","css","javascript"]}
{"index":{"_id":2}}
{"skills":["bootstrap", "angularjs", "backbone"]}
{"index":{"_id":3}}
{"skills":["python", "javascript", "ruby","java"]}

然后运行这个查询:

POST /test_index/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"script": {
"script": "count=0; for(s: doc['skills'].values){ for(x: skills){ if(s == x){ count +=1 } } } count >= 3",
"params": {
"skills": ["html", "css", "sass", "javascript", "django", "bootstrap", "angularjs", "backbone"]
}
}
}
}
}
}

并得到了我期望的结果:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"skills": [
"html",
"css",
"javascript"
]
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"skills": [
"bootstrap",
"angularjs",
"backbone"
]
}
}
]
}
}

下面是全部代码:

http://sense.qbox.io/gist/1018a01f1df29cb793ea15661f22bc8b25ed3476

关于Elasticsearch - 查找至少有 x 个元素与给定数组相同的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29499373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com