gpt4 book ai didi

elasticsearch - 弹性词查询的意外结果

转载 作者:行者123 更新时间:2023-12-03 00:41:08 26 4
gpt4 key购买 nike

我只有在http://localhost:9200上运行Elastic 2.4才能进行测试。

建立

作为新的开始,我在索引中创建了1个项目,但只有1个项目。

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

退货
{"_index":"movies","_type":"movie","_id":"1","_version":3,"_shards":{"total":2,"successful":1,"failed":0},"created":false}

然后,我运行以下命令以确认索引有效:
$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
"query": {
"query_string": {
"query": "Godfather"
}
}
}'

退货
{"took":8,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.095891505,"hits":[{"_index":"movies","_type":"movie","_id":"1","_score":0.095891505,"_source":
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}}]}}

问题

我试图像这样运行术语查询:
$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
"query": {
"term": {"title": "The Godfather"}
}
}'

我应该得到1个结果,相反我得到了:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

我怎么了

最佳答案

建议使用类似于jay的match_phrase,或者您需要创建一个not_analyzed子字段(例如title.raw),如下所示:

$ curl -s -XPUT "http://localhost:9200/movies/_mapping/movie" -d'
{
"properties": {
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'

然后,您可以为文档重新索引以填充 title.raw:
$ curl -s -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'

最后,您的字词查询将在 title.raw子字段上工作:
$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
"query": {
"term": {"title.raw": "The Godfather"}
}
}'

关于elasticsearch - 弹性词查询的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866002/

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