gpt4 book ai didi

elasticsearch - ElasticSearch |如何搜索包含空格的字符串列表?

转载 作者:行者123 更新时间:2023-12-02 22:47:52 26 4
gpt4 key购买 nike

我正在构建一个应用程序,用户可以在其中输入他们的技能,公司可以搜索(使用ElasticSearch)具有特定技能的用户。

我创建一个像这样的索引:

client.indices.create({
index: "candidates",
body: {
mappings: {
candidate: {
properties: {
languages: {type: 'text'},
skills: {type: 'text'},
},
},
},
},
}, (err, data) => {
if (err) console.log('err ', err);
if (data) console.log('data ', data);
})
}

在下面的示例中,我想搜索具有“Facebook Ads”和“在线营销”技能的用户。

应该对结果进行排序,以使具有两个匹配项的用户位于顶部。
{
"index": "candidates",
"type": "candidate",
"size": 10000,
"body": {
"query": {
"bool": {
"must": [
{
"bool": {
"should": {
"terms": {
"skills": [
"facebook ads",
"online marketing"
]
}
}
}
}
]
}
}
}
}

以上查询返回零结果。

问题:
作为 explained here,我应该避免对 term字段使用 terms(或 text)。

问题:
如何实现将字符串数组(其中一些包含空格)作为输入并返回有序命中列表的搜索查询?
所谓排序命中,是指与查询中大多数技能匹配的用户应该位于顶部。

编辑

这是一个同时具有Facebook Ads和Google Ads技能的用户的示例:
{
"_index" : "candidates",
"_type" : "candidate",
"_id" : "2fbbd818-sdhkfgkjhg-3235465hgfds",
"_score" : 9.1202545,
"_source" : {
"skills" : [
"Online strategi",
"Facebook Ads",
"Google Ads"
],
"languages": [
"da",
"en"
]
}
},

搜索['Facebook Ads','Google Ads']应在顶部返回上述用户(与Facebook Ads和Google Ads匹配),但也应返回仅一个匹配项的用户。

最佳答案

好,这就是我所做的

1) created the mappings for the data
2) indexed 3 documents. One document is same one as you posted above and one
is completely irrelevant data, and the third document has one search field
matching, so less relevance than the first document but more relevance
than the other document
3) the search query

当我运行搜索时,最相关的文档显示在匹配度最高的顶部,然后是第二个文档。

另请注意,正如您期望的那样,我正在搜索查询中使用双引号和单引号传递多个字符串。您可以构建字符串数组或具有串联字符串的字符串(根据需要使用空格等)..应该可以

这是映射
  PUT ugi-index2
{
"mappings": {
"_doc": {
"properties":{
"skills": {"type": "text"},
"languages": {"type": "keyword"}
}
}
}
}

和我索引的三个文档
   POST /ugi-index2/_doc/3
{
"skills" : [
"no skill",
"Facebook ads",
"not related"
],
"languages": [
"ab",
"cd"
]

}

POST /ugi-index2/_doc/2
{
"skills" : [
"no skill",
"test skill",
"not related"
],
"languages": [
"ab",
"cd"
]

}




POST /ugi-index2/_doc/1
{
"skills" : [
"Online strategi",
"Facebook Ads",
"Google Ads"
],
"languages": [
"da",
"en"
]

}

和搜索查询
  GET /ugi-index2/_search
{
"query":{
"multi_match": {
"query": "'Online Strate', 'Facebook'",
"fields": ["skills"]
}
}
}

在上面的查询中查找带空格的多个字符串(用于搜索)

这是回应
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "ugi-index2",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"skills" : [
"Online strategi",
"Facebook Ads",
"Google Ads"
],
"languages" : [
"da",
"en"
]
}
},
{
"_index" : "ugi-index2",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"skills" : [
"no skill",
"Facebook ads",
"not related"
],
"languages" : [
"ab",
"cd"
]
}
}
]
}
}

关于elasticsearch - ElasticSearch |如何搜索包含空格的字符串列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56543578/

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