Reputation: 239
I am trying to implement elastic search into my application. Currently i have a scenario where i need to search based on some search value.
Here is my implementation
Mapping
PUT /freelancer_working_days
{
"mappings":{
"properties":{
"id": {"type": "text"},
"freelancer_id": {"type": "text"},
"working_days": {"type": "text"},
"working_time": {"type":"keyword"}
}
}
}
A Document that i inserted
PUT /freelancer_working_days/_doc/1
{
"id": "1",
"freelancer_id":"abc-215-aysg-uyt-",
"working_days":"{{\"days\":[1,4,3,5,7],\"time\":{\"to\":7,\"from\":12}}",
"working_time":"{\"1\":{\"to\":7,\"from\":12},\"3\":{\"to\":7,\"from\":12},\"4\":{\"to\":7,\"from\":12},\"5\":{\"to\":7,\"from\":12},\"7\":{\"to\":7,\"from\":12}}"
}
Now what am i trying to get is that based on working time i get this document back so i am trying this but it gives me 0 hits. What am i doing wrong?
GET /freelancer_working_days/_search
{
"query": {
"term": {
"working_time": "12"
}
}
}
If i analyze with default values like this
GET /freelancer_working_days/_analyze
{
"text": ["{\"1\":{\"to\":7,\"from\":12},\"3\":{\"to\":7,\"from\":12},\"4\":{\"to\":7,\"from\":12},\"5\":{\"to\":7,\"from\":12},\"7\":{\"to\":7,\"from\":12}}"],
}
I get tokens like :
{
"tokens" : [
{
"token" : "1",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<NUM>",
"position" : 0
},
{
"token" : "to",
"start_offset" : 7,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "7",
"start_offset" : 11,
"end_offset" : 12,
"type" : "<NUM>",
"position" : 2
},
...
{
"token" : "12",
"start_offset" : 112,
"end_offset" : 114,
"type" : "<NUM>",
"position" : 24
}
]
Upvotes: 0
Views: 51
Reputation: 3158
Your working_time field is defined with type keyword
, and the keyword
analyzer is a noop analyzer, which mean your entire working_time string
{"1":{"to":7,"from":12},"3":{"to":7,"from":12},"4":{"to":7,"from":12},"5":{"to":7,"from":12},"7":{"to":7,"from":12}}
has been indexed as a single token string, and that's reason why your term query with value "12" does not match with it.
Testing it with the _analyze
API on your freelancer_working_days
index
GET /freelancer_working_days/_analyze
{
"field": "working_time",
"text": "{\"1\":{\"to\":7,\"from\":12},\"3\":{\"to\":7,\"from\":12},\"4\":{\"to\":7,\"from\":12},\"5\":{\"to\":7,\"from\":12},\"7\":{\"to\":7,\"from\":12}}"
}
will give
{
"tokens" : [
{
"token" : """{"1":{"to":7,"from":12},"3":{"to":7,"from":12},"4":{"to":7,"from":12},"5":{"to":7,"from":12},"7":{"to":7,"from":12}}""",
"start_offset" : 0,
"end_offset" : 116,
"type" : "word",
"position" : 0
}
]
}
Reference: Keyword analyzer
If you need a full text search, consider to change the type of working_time
field to text
Upvotes: 1