Elasticsearch基于字符串出现在数组中的出现次数进行排序

时间:2013-03-11 03:54:49

标签: search lucene elasticsearch tire

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

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

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

结果应包括: 文件1:[“CA”,“NY”,“NY”] 文件2:[“NY”,FL“] 文件3:[“NY”,CA“,”NY“,”NY“]

结果应该按此订购

用户3,用户1,用户2

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

2 个答案:

答案 0 :(得分:0)

这将通过标准的Lucene评分实现来实现。如果您只是在不指定订单的情况下搜索“NY”,它将按相关性进行排序,并将为文档指定最高相关性,其中所有其他条件相同。

答案 1 :(得分:0)

对于那些好奇的人,我无法根据数组中出现的单词数量来提升。然而,我确实完成了以下所需的工作:

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