1

我正在构建一个应用程序,用户可以在其中输入他们的技能,公司可以搜索(使用 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 广告”和“在线营销”技能的用户。

结果应该被排序,所以有两个匹配的用户应该在顶部。

{
  "index": "candidates",
  "type": "candidate",
  "size": 10000,
  "body": {
    "query": {
      "bool": {
        "must": [
          {
            "bool": {
              "should": {
                "terms": {
                  "skills": [
                    "facebook ads",
                    "online marketing"
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
}

上述查询返回零个结果。

问题:正如这里所解释的,我应该避免使用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),但也应该返回只有一个匹配项的用户。

4

2 回答 2

2

好的,这就是我所做的

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"
          ]
        }
      }
    ]
  }
}
于 2019-06-11T13:01:40.073 回答
1

如果您想匹配确切的术语,您需要确保您还将技能存储为关键字。这将使空间保持不变并允许精确匹配。在用户界面中使用此功能的常用方法是提供带有关键字数据的过滤器作为预定义的过滤器选项。

如果您仍想使用用户可以提供任意搜索数据的全文搜索,您可以依靠这样一个事实,即包含“Facebook”和“广告”的文档将返回比仅包含“Facebook”的文档更高的分数。

于 2019-06-11T12:32:36.333 回答