0

我有一个包含字符串列表的数组字段:即:[“NY”,“CA”]

在搜索时,我有一个匹配数组中任何字符串的过滤器。

我想根据搜索字符串出现次数最多的文档对结果进行排序:“NY”

结果应包括: 文件 1:["CA"、"NY"、"NY"] 文件 2:["NY"、FL"] 文件 3:["NY"、CA"、"NY"、"NY"]

结果应该这样排序

用户 3、用户 1、用户 2

这可能吗?如果是这样,怎么做?

4

2 回答 2

1

对于那些好奇的人,我无法根据数组中单词出现的次数来提升。但是,我确实通过以下方式完成了我所需要的:

curl -X POST "http://localhost:9200/index/document/1" -d '{"id":1,"states_ties":["CA"],"state_abbreviation":"CA","worked_in_states":["CA"],"training_in_states":["CA"]}'
curl -X POST "http://localhost:9200/index/document/2" -d '{"id":2,"states_ties":["CA","NY"],"state_abbreviation":"FL","worked_in_states":["NY","CA"],"training_in_states":["NY","CA"]}'
curl -X POST "http://localhost:9200/index/document/3" -d '{"id":3,"states_ties":["CA","NY","FL"],"state_abbreviation":"NY","worked_in_states":["NY","CA"],"training_in_states":["NY","FL"]}'

curl -X GET 'http://localhost:9200/index/_search?per_page=10&pretty' -d '{
  "query": {
    "custom_filters_score": {
      "query": {
        "terms": {
          "states_ties": [
            "CA"
          ]
        }
      },
      "filters": [
        {
          "filter": {
            "term": {
              "state_abbreviation": "CA"
            }
          },
          "boost": 1.03
        },
        {
          "filter": {
            "terms": {
              "worked_in_states": [
                "CA"
              ]
            }
          },
          "boost": 1.02
        },
        {
          "filter": {
            "terms": {
              "training_in_states": [
                "CA"
              ]
            }
          },
          "boost": 1.01
        }
      ],
      "score_mode": "multiply"
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

results: id: score

1: 0.75584483
2: 0.73383
3: 0.7265643
于 2013-03-12T22:04:14.543 回答
0

这将通过标准的 Lucene 评分实现来完成。如果您只是在不指定顺序的情况下搜索“NY”,它将按相关性排序,并将最高相关性分配给该术语出现次数较多的文档,其他条件相同。

于 2013-03-11T16:19:43.443 回答