elasticsearch 弹性术语查询的意外结果
我有弹性2.4只运行测试 安装程序 作为一个全新的开始,我在索引中创建了1个项目,而且只有1个项目
elasticsearch 弹性术语查询的意外结果,
elasticsearch,
elasticsearch,我有弹性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":"
$ 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}
{"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": {
"query_string": {
"query": "Godfather"
}
}
}'
返回
{"_index":"movies","_type":"movie","_id":"1","_version":3,"_shards":{"total":2,"successful":1,"failed":0},"created":false}
{"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"}
}
}'
$ curl -s -XPUT "http://localhost:9200/movies/_mapping/movie" -d'
{
"properties": {
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
我本应得到1个结果,但我得到了以下结果:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
我做错了什么?要么像jay建议的那样匹配短语,要么你需要创建一个
未分析的子字段(例如title.raw
),如下所示:
$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
"query": {
"term": {"title": "The Godfather"}
}
}'
$ curl -s -XPUT "http://localhost:9200/movies/_mapping/movie" -d'
{
"properties": {
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
然后,您可以重新索引文档以填充标题。原始:
$ 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"}
}
}'
要么像jay建议的那样匹配短语
,要么需要创建一个未分析
子字段(例如title.raw
),如下所示:
$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
"query": {
"term": {"title": "The Godfather"}
}
}'
$ curl -s -XPUT "http://localhost:9200/movies/_mapping/movie" -d'
{
"properties": {
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
然后,您可以重新索引文档以填充标题。原始:
$ 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"}
}
}'
已分析标题
字段,因此请尝试匹配
查询,而不是术语
;-)谢谢我想找到与标题
完全匹配的“教父”
,因此匹配
似乎不适合我的需要。我是否可以使用给定的数据进行术语查询?请尝试使用匹配短语。已分析标题
字段,因此请尝试匹配
查询,而不是术语
;-)谢谢我想找到与标题
完全匹配的“教父”
,因此匹配
似乎不适合我的需要。我是否可以使用给定的数据进行术语查询?请尝试使用匹配短语。