Skip to content

Instantly share code, notes, and snippets.

@tarnacious
Last active December 18, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarnacious/f8d5cebcc3132d898ecd to your computer and use it in GitHub Desktop.
Save tarnacious/f8d5cebcc3132d898ecd to your computer and use it in GitHub Desktop.
Strange elasticsearch search behaviour?
In tracking down a bug in an application, I found this behaviour in
elasticsearch which is not what I would expect.
It seems that adding a new mapping type somehow breaks the analyzers in
another type. I see the behaviour in elasticsearch 1.4.4 which we are using and
in 1.7.4.
Can someone explain what is going on here? Am I doing something wrong?
{"acknowledged":true}
{"acknowledged":true}
{"_index":"text-index","_type":"user","_id":"AVG0hasF-Z9PaJz0rP2W","_version":1,"created":true}
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"text-index","_type":"user","_id":"AVG0hasF-Z9PaJz0rP2W","_score":0.30685282,"_source":
{
"address":
{
"description": "Walked"
}
}}]}}
{"acknowledged":true}
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
ELASTICSEARCH=localhost:9200
INDEX=text-index
# Clear the index
curl -XDELETE $ELASTICSEARCH/$INDEX/
echo
# Create and index
curl -XPUT -H "Content-Type: json" $ELASTICSEARCH/$INDEX/ -d '
{
"settings": {
"index": {
"number_of_shards": 1,
"analysis": {
"filter": {
"stemmer": {
"type": "snowball",
"language": "English"
}
},
"analyzer": {
"custom": {
"type": "custom",
"tokenizer": "standard",
"filter": "stemmer"
}
}
},
"number_of_replicas": 0
}
},
"mappings": {
"user": {
"dynamic": "strict",
"properties": {
"address": {
"properties": {
"description": {
"type": "string",
"analyzer": "custom",
"store": "yes"
}
}
}
}
}
}
}
}'
echo
# Index a document
curl -XPOST -H "Content-Type: json" $ELASTICSEARCH/$INDEX/user -d '
{
"address":
{
"description": "Walked"
}
}'
echo
sleep 1
# Search, should find something
curl -XPOST -H "Content-Type: json" $ELASTICSEARCH/$INDEX/user/_search -d '
{
"query": {
"filtered": {
"query": {
"simple_query_string": {
"default_operator": "AND",
"fields": [
"user.address.description"
],
"query": "Walk"
}
}
}
}
}'
echo
# Add a mapping called address, which happens to be the name of field a nested datatype
curl -XPUT -H "Content-Type: json" $ELASTICSEARCH/$INDEX/_mapping/address -d '
{
"dynamic": "strict"
}'
echo
sleep 1
# Search, now returns no results!
curl -XPOST -H "Content-Type: json" $ELASTICSEARCH/$INDEX/user/_search -d '
{
"query": {
"filtered": {
"query": {
"simple_query_string": {
"default_operator": "AND",
"fields": [
"user.address.description"
],
"query": "Walk"
}
}
}
}
}'
echo
{"acknowledged":true}
{"acknowledged":true}
{"_index":"text-index","_type":"user","_id":"AVG0hasF-Z9PaJz0rP2W","_version":1,"created":true}
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"text-index","_type":"user","_id":"AVG0hasF-Z9PaJz0rP2W","_score":0.30685282,"_source":
{
"address":
{
"description": "Walked"
}
}}]}}
{"acknowledged":true}
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"text-index","_type":"user","_id":"AVG0hasF-Z9PaJz0rP2W","_score":0.30685282,"_source":
{
"address":
{
"description": "Walked"
}
}}]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment