Skip to content

Instantly share code, notes, and snippets.

@resetdel
Created February 21, 2011 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save resetdel/837120 to your computer and use it in GitHub Desktop.
Save resetdel/837120 to your computer and use it in GitHub Desktop.
ES: demo for highlight bug
curl -XPUT 'http://localhost:9200/testme/testme' -d '{
streamType: "Feed",
tenantId: "1",
message: "bla bla bla post bla bla 1 bla",
description: "Description",
postDate:"2010-06-03T21:11:09+0300"
}'
curl -XPOST 'http://localhost:9200/testme/testme/_search' -d '{ sort:{postDate:{reverse:true}}, size:100, query : { bool : {must : [ {wildcard : { message : "*post*" }}, {term : { tenantId : "1" }} ] }} ,"highlight" : { "pre_tags" : ["<em class='highlight'>"], "post_tags" : ["</em>"], "fields" : {"message" : {"fragment_size" : 200, "number_of_fragments" : 1}}}}'
Response:
{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":null,"hits":[{"_index":"testme","_type":"testme","_id":"1gf-JGBaTHKIiIUzBRCKNw","_score":null, "_source" : {
streamType: "Feed",
tenantId: "1",
message: "bla bla bla post bla bla 1 bla",
description: "Description",
postDate:"2010-06-03T21:11:09+0300"
},"highlight":{"message":["bla bla bla <em class='highlight'>post</em> bla bla <em class='highlight'>1</em> bla"]},"sort":[1275588669000]}]}}
@clintongormley
Copy link

The problem is that tenantId: 1 is part of your query, when actually you want to filter by that field.

Try this instead:

# [Mon Feb 21 15:49:45 2011] Protocol: http, Server: 127.0.0.1:9200
curl -XPOST 'http://127.0.0.1:9200/testme/testme'  -d '
{
   "tenantId" : "1",
   "postDate" : "2010-06-03T21:11:09+0300",
   "streamType" : "Feed",
   "description" : "Description",
   "message" : "bla bla bla post bla bla 1 bla"
}
'

# [Mon Feb 21 15:49:45 2011] Response:
# {
#    "ok" : true,
#    "_index" : "testme",
#    "_id" : "pLQkX7JwScWDJmZ-ZDoRkQ",
#    "_type" : "testme"
# }

# [Mon Feb 21 15:49:50 2011] Protocol: http, Server: 127.0.0.1:9200
curl -XGET 'http://127.0.0.1:9200/testme/_search'  -d '
{
   "sort" : {
      "postDate" : {
         "reverse" : "true"
      }
   },
   "query" : {
      "filtered" : {
         "query" : {
            "field" : {
               "message" : "post*"
            }
         },
         "filter" : {
            "term" : {
               "tenantId" : 1
            }
         }
      }
   },
   "highlight" : {
      "fields" : {
         "message" : {
            "fragment_size" : 200,
            "number_of_fragments" : 1
         }
      },
      "post_tags" : [
         "</em>"
      ],
      "pre_tags" : [
         "<em class=\u0027highlight\u0027>"
      ]
   },
   "size" : 100
}
'

# [Mon Feb 21 15:49:50 2011] Response:
# {
#    "hits" : {
#       "hits" : [
#          {
#             "_source" : {
#                "tenantId" : "1",
#                "streamType" : "Feed",
#                "postDate" : "2010-06-03T21:11:09+0300",
#                "description" : "Description",
#                "message" : "bla bla bla post bla bla 1 bla"
#             },
#             "sort" : [
#                1275588669000
#             ],
#             "_score" : null,
#             "_index" : "testme",
#             "_id" : "pLQkX7JwScWDJmZ-ZDoRkQ",
#             "_type" : "testme",
#             "highlight" : {
#                "message" : [
#                   "bla bla bla <em class='highlight'>post</em> bl
# >                   a bla 1 bla"
#                ]
#             }
#          }
#       ],
#       "max_score" : null,
#       "total" : 1
#    },
#    "_shards" : {
#       "failed" : 0,
#       "successful" : 5,
#       "total" : 5
#    },
#    "took" : 2
# }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment