Reputation: 5323
Using
$ docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2
Populate db
$ curl -X PUT "localhost:9200/my_index/my_type/1" -H 'Content-Type: application/json' -d'
{
"text": "foo bar"
}
'
$ curl -X PUT "localhost:9200/my_index/my_type/2" -H 'Content-Type: application/json' -d'
{
"text": "baz quix"
}
'
Validate that db was populated
$ curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'
{"took":48,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"my_index","_type":"my_type","_id":"2","_score":1.0,"_source":
{
"text": "baz quix"
}
},{"_index":"my_index","_type":"my_type","_id":"1","_score":1.0,"_source":
{
"text": "foo bar"
}
}]}}
constant_score
returns []
, though expected to return object with id 2
$ curl -X GET "localhost:9200/my_index/my_type/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"constant_score": {
"filter": {
"term": {
"text":"baz quix"
}
}
}
}
}'
{"took":14,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
filtered
is not supported at all
$ curl -X GET "localhost:9200/my_index/my_type/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"filtered": {
"filter": {
"term": {
"text":"baz quix"
}
}
}
}
}'
{"error":{"root_cause":[{"type":"parsing_exception","reason":"no [query] registered for [filtered]","line":4,"col":17}],"type":"parsing_exception","reason":"no [query] registered for [filtered]","line":4,"col":17},"status":400}
Neither search without constant_score
not working
$ curl -X GET "localhost:9200/my_index/my_type/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"text":"baz quix"
}
}
}'
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
$ curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"text":"baz quix"
}
}
}'
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
Upvotes: 0
Views: 420
Reputation: 1123
First of all, examine the mapping of your index:
$ curl -X GET -H 'Content-Type: application/json' -i 'http://localhost:9200/my_index/_mapping'
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
In that output you can see that Elasticsearch by default maps your field twice: it creates field named "text"
with "type": "text"
and nested field named "text.keyword"
with "type": "keyword"
.
Fields with type text
are full text fields, which data are analyzed while indexing, and fields with type keyword
persist their data in index "as is" without being analyzed.
So in your case for term query you must use nested field with type keyword
:
$ curl -X GET "localhost:9200/my_index/my_type/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"constant_score": {
"filter": {
"term": {
"text.keyword": "baz quix"
}
}
}
}
}'
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"my_index","_type":"my_type","_id":"2","_score":1.0,"_source":{"text":"baz quix"}}]}}
Also you can read section "Why doesn’t the term query match my document?" in official documentation, which also provides clean explanation.
Upvotes: 1