Hi Nick,
In the example you provide, you the tags-field will contain the term "met"
after being analyzed with the standard analyzer, and while you're
specifying a custom case sensitive analyzer at query-time, your search term
is "met" in lower-case, which naturally matches the indexed term "met". If
you had searched for "MET" instead, you would have gotten zero results.
If you need to be able to sometimes search for terms that are case
sensitive, you should prepare for this index-time, using the multi-field
type and create a new "virtual" field that is analyzed the way you want. I
prepared a small "play" that demonstrates this using three different search
requests here: https://www.found.no/play/gist/7870684, which you can run
against your own cluster to verify:
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Create indexes
curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"settings": {
"analysis": {
"analyzer": {
"casesensitive": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"standard",
"stop"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"title": {
"type": "multi_field",
"fields": {
"title": {
"type": "string"
},
"title.cased": {
"type": "string",
"analyzer": "casesensitive"
}
}
},
"tags": {
"type": "multi_field",
"fields": {
"tags": {
"type": "string"
},
"tags.cased": {
"type": "string",
"analyzer": "casesensitive"
}
}
}
}
}
}
}'
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"title":"Metastatic Lung Cancer","tags":["MET","Lung","HGF"]}
{"index":{"_index":"play","_type":"type"}}
{"title":"Colorectal kras c.12345G>T","tags":["PTEN
Loss","colorectal","KRAS"]}
{"index":{"_index":"play","_type":"type"}}
{"title":"Colorectal Kras","tags":["colorectal","KRAS c.12345G>T"]}
'
# Do searches
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"filter": {
"query": {
"multi_match": {
"fields": [
"title",
"tags"
],
"query": "met",
"type": "phrase",
"analyzer": "casesensitive"
}
}
}
}
'
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"filter": {
"query": {
"multi_match": {
"fields": [
"title",
"tags"
],
"query": "MET",
"type": "phrase",
"analyzer": "casesensitive"
}
}
}
}
'
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"filter": {
"query": {
"multi_match": {
"fields": [
"title.cased",
"tags.cased"
],
"query": "met",
"type": "phrase",
"analyzer": "casesensitive"
}
}
}
}
'
Sincerely,
Njal Karevoll
On Saturday, December 7, 2013 11:30:33 PM UTC+1, Nick Tackes wrote:
Hi,
I have attached a simple example of an index that defines a custom
analyzer called casesensitive. I want the default behavior for the index
to be the standard index, however, I would like to be able to do a
casesensitive query at certain times. Can I define an analyzer as shown
below just for use in a multi match query? In the example below I would
expect no hits yet i still get the case insensitive hit returning.
thanks
curl -X DELETE localhost:9200/mhw_test
curl -X PUT localhost:9200/mhw_test -d '
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"casesensitive": {
"type":"custom",
"tokenizer": "standard",
"filter": ["standard", "stop"]
}
}
}
}
},
"mappings":{"test":{"properties":{
"title":{"type":"string"},
"tags":{"type":"string"}
}
}
}
}'
curl -X POST "http://localhost:9200/mhw_test/test" -d '
{
"title":"Metastatic Lung Cancer",
"tags":["MET","Lung","HGF"]
}'
curl -X POST "http://localhost:9200/mhw_test/test" -d '
{
"title":"Colorectal kras c.12345G>T",
"tags":["PTEN Loss","colorectal","KRAS"]
}'
curl -X POST "http://localhost:9200/mhw_test/test" -d '
{
"title":"Colorectal Kras",
"tags":["colorectal","KRAS c.12345G>T"]
}'
curl -XPOST 'http://localhost:9200/mhw_test/_refresh'
curl -X POST "http://localhost:9200/mhw_test/test/_search" -d '
{"filter":{
"query" : {
"multi_match" : {
"fields":["title", "tags"],
"query" : "met",
"type" : "phrase",
"analyzer": "casesensitive"
}
}
}
}'
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/6bf67d29-a861-48f6-9b6d-2dd2245266c1%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.