如何使用ElasticSearch正确过滤/排序?

时间:2015-10-05 15:15:00

标签: elasticsearch

我刚刚使用本教程创建了一些非常简单的“电影”数据库(索引):http://joelabrahamsson.com/elasticsearch-101/

现在,我尝试复制/粘贴指令,为“director”字段创建多字段映射:

    curl -XPUT "http://localhost:9200/movies/movie/_mapping" -d'
{
   "movie": {
      "properties": {
         "director": {
            "type": "multi_field",
            "fields": {
                "director": {"type": "string"},
                "original": {"type" : "string", "index" : "not_analyzed"}
            }
         }
      }
   }
}'

但在此之后,如果我发布此查询,我得不到结果:

curl -XPOST "http://localhost:9200/_search" -d'
{
    "query": {
        "constant_score": {
            "filter": {
                "term": { "director.original": "Francis Ford Coppola" }
            }
        }
    }
}'

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

如果我尝试使用此排序:

http://localhost:9200/movies/movie/_search?sort=title.original:asc

我以随机顺序得到整个表(类型)(与没有“sort”指令的顺序相同):

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":6,"max_score":null,"hits":[{"_index":"movies","_type":"movie","_id":"4","_score":null,"_source":
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "War"]
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"5","_score":null,"_source":
{
    "title": "Kill Bill: Vol. 1",
    "director": "Quentin Tarantino",
    "year": 2003,
    "genres": ["Action", "Crime", "Thriller"]
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"1","_score":null,"_source":
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"6","_score":null,"_source":
{
    "title": "The Assassination of Jesse James by the Coward Robert Ford",
    "director": "Andrew Dominik",
    "year": 2007,
    "genres": ["Biography", "Crime", "Drama"]
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"2","_score":null,"_source":
{
    "title": "Lawrence of Arabia",
    "director": "David Lean",
    "year": 1962,
    "genres": ["Adventure", "Biography", "Drama"]
},"sort":[null]},{"_index":"movies","_type":"movie","_id":"3","_score":null,"_source":
{
    "title": "To Kill a Mockingbird",
    "director": "Robert Mulligan",
    "year": 1962,
    "genres": ["Crime", "Drama", "Mystery"]
},"sort":[null]}]}}

那么你能告诉我在ElasticSearch的这个基本用法中我缺少什么吗?为什么我的自定义“导演”字段没有过滤或排序?

1 个答案:

答案 0 :(得分:2)

您没有正确创建多字段。你应该这样做:

curl -XPOST "http://localhost:9200/movies/movie/_mapping" -d '{
   "movie": {
      "properties": {
         "director": {
            "type": "string",
            "fields": {
                "original": {"type" : "string", "index" : "not_analyzed"}
            }
         }
      }
   }
}'

另请注意,在该教程中,他们使用了一种不推荐使用的方式来声明多字段,即使用"type": "multi_field"。现在我们按照上面的方式进行操作。

以下编辑表单注释:将映射更改为多字段后,您需要重新运行6个索引查询以重新索引六部电影,以便填充director.original字段。