gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:04:13 25 4
gpt4 key购买 nike

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

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

    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的这种基本用法缺少什么吗?为什么不对我的自定义“导演”字段进行过滤或排序?

最佳答案

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

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

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

在下面的EDIT表单注释中:将映射更改为多字段后,您需要重新运行6个索引查询以重新索引6部电影,以便Director.original字段被填充。

关于elasticsearch - 如何使用ElasticSearch正确过滤/排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32952018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com