Skip to content

Instantly share code, notes, and snippets.

Created March 4, 2011 01:36
Show Gist options
  • Save anonymous/853994 to your computer and use it in GitHub Desktop.
Save anonymous/853994 to your computer and use it in GitHub Desktop.
# curl -XDELETE http://localhost:9200/test-index
# "analyzer"."default" => default name for index and search
# "tokenizer" : "standard" => splits words at punctuation characters
# http://www.elasticsearch.org/guide/reference/index-modules/analysis/
curl -XPUT http://localhost:9200/test-index/ -d '
{
"index": {
"analysis": {
"analyzer": {
"default": {
"tokenizer": "standard",
"filter": ["lowercase", "snowball"]
}
}
}
}
}'
# http://www.elasticsearch.org/guide/reference/api/search/highlighting.html
# "store": "yes" => enable highlighting
# "term_vector" : "with_positions_offsets" => for performance
curl -XPUT http://localhost:9200/test-index/test-item/_mapping -d '
{
"test-item": {
"properties": {
"name": {
"type": "string",
"store": "yes",
"term_vector": "with_positions_offsets"
},
"description": {
"type": "string",
"store": "yes",
"term_vector": "with_positions_offsets"
},
"field-without-highlighting": {
"type": "string"
}
}
}
}'
curl -XPUT http://localhost:9200/test-index/test-item/1 -d '
{
"name": "Example One",
"description": "Create a test item for to the index.",
"field-without-highlighting": "test"
}'
# "highlight" => wrap search result in <em> tags (define own tags with pre_tags/post_tags)
# "number_of_fragments": 0 => don"t split field in multiple fragments
# http://www.elasticsearch.org/guide/reference/api/search/highlighting.html
curl -XGET http://localhost:9200/test-index/test-item/_search?pretty=true -d '
{
"highlight": {
"fields": {
"name": {
"number_of_fragments": 0
},
"description": {
"number_of_fragments": 0
}
}
},
"query": {
"query_string": {
"fields": ["name", "description"],
"query": "test"
}
}
}'
=>
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.029424578,
"hits": [{
"_index": "test-index",
"_type": "test-item",
"_id": "1",
"_score": 0.029424578,
"_source": {
"name": "Example One",
"description": "Create a test item for to the index.",
"field-without-highlighting": "test"
},
"highlight": {
"description": ["Create a <em>test</em> item for to the index."]
}
}]
}
}
curl -XGET http://localhost:9200/test-index/test-item/_search?pretty=true -d '
{
"highlight": {
"fields": {
"name": {
"number_of_fragments": 0
},
"description": {
"number_of_fragments": 0
}
}
},
"query": {
"query_string": {
"fields": ["name", "description"],
"query": "created EXAMPLES"
}
}
}'
=>
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.06241896,
"hits": [{
"_index": "test-index",
"_type": "test-item",
"_id": "1",
"_score": 0.06241896,
"_source": {
"name": "Example One",
"description": "Create a test item for to the index.",
"field-without-highlighting": "test"
},
"highlight": {
"description": ["<em>Create</em> a test item for to the index."],
"name": ["<em>Example</em> One"]
}
}]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment