如何根据字词与关键字的相关性对字词进行排序?

时间:2017-12-07 03:58:07

标签: arrays json sorting elasticsearch

我目前正在使用elasticsearch进行搜索。我们有非常多的用户。

以下是elasticsearch地图:

PUT /example_index/_mapping/users
{
  "properties": {
    "user_autocomplete": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        },
        "completion": {
          "type": "text",
          "analyzer": "user_autocomplete_analyzer",
          "search_analyzer": "standard"
        }
      }
    },
    "firstName": {
      "type": "text"
    },
    "lastName": {
      "type": "text"
    }
  }
}

这是搜索查询。 例如,我得到3条记录

GET example_index/users/_search
{
  "from": 0,
  "size": 3,
  "query": {
    "query_string": {
      "query": "*ro*",
      "fields": [
        "firstName",
        "lastName"
      ]
    }
  },
  "aggs": {
    "user_suggestions": {
      "terms": {
        "size": 3,
        "field": "user_autocomplete.raw"
      }
    }
  }
}

这是elasticsearch的输出

{
  "took": 53,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 1,
    "hits": [
      {
        "_index": "example_index",
        "_type": "users",
        "_id": "08",
        "_score": 1,
        "_source": {
          "firstName": "Eero",
          "lastName": "Saarinen",
          "user_autocomplete": "Eero Saarinen"
        }
      },
      {
        "_index": "example_index",
        "_type": "users",
        "_id": "16",
        "_score": 1,
        "_source": {
          "firstName": "Aaron",
          "lastName": "Judge",
          "user_autocomplete": "Aaron Judge"
        }
      },
      {
        "_index": "example_index",
        "_type": "users",
        "_id": "20",
        "_score": 1,
        "_source": {
          "firstName": "Robert",
          "lastName": "Langdon",
          "user_autocomplete": "Robert Langdon"
        }
      }
    ]
  },
  "aggregations": {
    "user_suggestions": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 10,
      "buckets": [
        {
          "key": "Eero Saarinen",
          "doc_count": 1
        },
        {
          "key": "Aaron Judge",
          "doc_count": 1
        },
        {
          "key": "Robert Langdon",
          "doc_count": 1
        }
      ]
    }
  }
}

我需要按以下顺序搜索结果:

  1. Robert Langdon

  2. Aaron Judge

  3. Eero Saarinen

  4. 我试过订购方法。它不会起作用。有没有办法做到这一点?

0 个答案:

没有答案