Terms facet not working with not_analyzed fields and dynamic template


(Mahesh Kommareddi) #1

Hi all.

I've been trying to get a terms facet working along side not_analyzed
fields an I'm at a bit of a loss about how to get some of this worked out.

First, I'm using a dynamic template, that allows for multi_field and where
the fields have an analyzed section and an not_analyzed section.

You can see and try the mapping here:

I put in some test data:

Now I try searching against the analyzed field (field1) and it works as I
would think:
curl -XGET localhost:9200/mtest/dynamics/_search?q=field1:Words

{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":1,"max_score":0.19178301,"hits":[{"_index":"mtest","_type":
"dynamics","_id":"1","_score":0.19178301, "_source" : {"field1" : "Words
are nice", "field2": "We are here"}}]}}

I try searching field1 again with a different term that appears twice and
it works as I would expect
curl -XGET localhost:9200/mtest/dynamics/_search?q=field1:Word

{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":2,"max_score":0.30685282,"hits":[{"_index":"mtest","_type":
"dynamics","_id":"3","_score":0.30685282, "_source" : {"field1" : "Word",
"field2": "not a match"}},"_index":"mtest","_type":"dynamics","_id":"4",
"_score":0.30685282, "_source" : {"field1" : "Word", "field2": "match"}}]}}

If I search for the single word with the not_analyzed field, I get no
matches. Something I don't expect. No hits:
curl -XGET localhost:9200/mtest/dynamics/_search?q=field1.org:Word

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":0,"max_score":null,"hits":[]}}

However, if I search on whole phrases, I get a hit:
curl -XGET localhost:9200/mtest/dynamics/_search?q=field1.org:Words%20are%
20nice

{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":1,"max_score":0.02250402,"hits":[{"_index":"mtest","_type":
"dynamics","_id":"1","_score":0.02250402, "_source" : {"field1" : "Words
are nice", "field2": "We are here"}}]}}

So Question 1 is: How do I get hits on single words for not_analyzed fields?

Question 2 is about the terms facet relating to this mapping / not_analyed
setting.

If I do a match_all against all the items and run a terms facet, I get the
tokenized results I expect:

curl -XGET localhost:9200/mtest/dynamics/_search -d '{
"query" : {
"match_all": {}
},
"facets" : {
"topFieldElements" : {
"terms" : {
"field" : "field1",
"size" : 10
}
}
}
}'

... "facets":{"topFieldElements":{"_type":"terms","missing":0,"total":5,
"other":0,"terms":[{"term":"word","count":2},{"term":"words","count":1},{
"term":"sentence","count":1},{"term":"nice","count":1}]}}}

However, running the terms facet on not_analyzed fields results in no
information (missing: 4):
curl -XGET localhost:9200/mtest/dynamics/_search -d '{
"query" : {
"match_all": {}
},
"facets" : {
"topFieldElements" : {
"terms" : {
"field" : "field1.org",
"size" : 10
}
}
}
}'

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":4,"max_score":1.0,"hits":[{"_index":"mtest","_type":
"dynamics","_id":"3","_score":1.0, "_source" : {"field1" : "Word", "field2": "not
a match"}},{"_index":"mtest","_type":"dynamics","_id":"2","_score":1.0,
"_source" : {"field1" : "this is a sentence", "field2": "We are here"}},{
"_index":"mtest","_type":"dynamics","_id":"4","_score":1.0, "_source" : {
"field1" : "Word", "field2": "match"}},{"_index":"mtest","_type":"dynamics",
"_id":"1","_score":1.0, "_source" : {"field1" : "Words are nice", "field2": "We
are here"}}]},"facets":{"topFieldElements":{"_type":"terms","missing":4,
"total":0,"other":0,"terms":[]}}}

This appears to be the case in any amount of data (?). How can I get terms
data in not_analyzed fields?

Thanks!

--
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/bf270a25-60d8-4f03-b2ed-d2dc568748b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #2

Just FYI, the dynamic mapping applies only to fields that you have no
explicit mappings defined. In your case, you predefined field1 and field2
as type string so the dynamic mapping will ignore those 2 fields and will
not be applied to them. That is probably the cause of your query behaviors.

If you remove field1 and field2 from your initial mapping, I think you
should get what you are expecting.

--
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/afdbdac5-7315-42ef-a2b9-c30a488408ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Mahesh Kommareddi) #3

This seemed to be the case! Using the mapping you suggested (below) which
made the other search and facet queries work the I wanted them to.

curl -XPUT localhost:9200/mtest
curl -X POST localhost:9200/mtest/dynamics/_mapping -d '
{
"dynamic":{
"dynamic_templates":[
{
"template_1":{
"match":"*",
"mapping":{
"type":"multi_field",
"fields":{
"{name}":{
"type":"string",
"index":"analyzed"
},
"org":{
"type":"string",
"index":"not_analyzed"
}
}
}
}
}
]
}
}'

Thanks, again, Binh.

Mahesh

On Tuesday, March 18, 2014 7:57:47 PM UTC-4, Binh Ly wrote:

Just FYI, the dynamic mapping applies only to fields that you have no
explicit mappings defined. In your case, you predefined field1 and field2
as type string so the dynamic mapping will ignore those 2 fields and will
not be applied to them. That is probably the cause of your query behaviors.

If you remove field1 and field2 from your initial mapping, I think you
should get what you are expecting.

--
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/e489a762-21b3-4aca-8d29-ed0199a095b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4