按术语频率计数排序结果

时间:2016-04-29 13:42:17

标签: sorting elasticsearch

如果有2个文件中有“世界”字样5次&分别为2次。

所以我希望首先列出5次单词“world”的文档,然后是2次单词“world”的文档。

我如何排序?

感谢。

1 个答案:

答案 0 :(得分:0)

我认为没有必要对它进行排序。如果你有你提到的文件,并且你正在搜索一个特定的单词,在你的情况下看起来超过一个,两个或三个,弹性搜索将自动计算它的分数,并将通过分数排序返回文档。

尝试这个摄取一些文件:

curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
  "title": "The Godfather",
  "director": "Francis Ford Coppola",
  "year": 1972,
  "genres": [
    "Crime",
    "Drama"
  ]
}'

curl -XPUT "http://localhost:9200/movies/movie/2" -d'
{
  "title": "The Godfather Godfather",
  "director": "Francis Ford Coppola",
  "year": 1972,
  "genres": [
    "Crime",
    "Drama"
  ]
}'

curl -XPUT "http://localhost:9200/movies/movie/3" -d'
{
  "title": "The Godfather Godfather Godfather",
  "director": "Francis Ford Coppola",
  "year": 1972,
  "genres": [
    "Crime",
    "Drama"
  ]
}'

摄取后运行此查询并查看结果:

curl -XPOST "http://localhost:9200/movies/_search" -d'
{
  "explain": true,
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "godfather"
        }
      }
    }
  }
}'

这将使文件返回顶部,因为它有"教父"多次