Created
August 3, 2011 09:37
-
-
Save anonymous/1122262 to your computer and use it in GitHub Desktop.
Exxample of faceted search with filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### (1) mappings properties. two field for tags (one analyzed with semicolon (to calculate facets) and one normal | |
curl -POST 'http://localhost:9200/test' -d ' | |
{ | |
"settings":{ | |
"analysis": { | |
"analyzer": { | |
"semicolon":{ | |
"type": "pattern", | |
"pattern":";" | |
} | |
} | |
} | |
}, | |
"mappings" : { | |
"news" : { | |
"properties" : { | |
"tags_analyzed" : { | |
"type" : "string", | |
"analyzer": "semicolon" | |
} | |
} | |
} | |
} | |
}' | |
#### (2) index some items | |
curl -XPOST 'http://localhost:9200/test/news/1' -d ' | |
{ "title": "Example 1", | |
"description": "In this year...", | |
"tags": "Europe;West Asia;money;year", | |
"tags_analyzed": "Europe;West Asia;money;year" | |
} | |
' | |
curl -XPOST 'http://localhost:9200/test/news/2' -d ' | |
{ "title": "Example 2", | |
"description": "While in most of the...", | |
"tags": "Europe;West Asia;class;flights", | |
"tags_analyzed": "Europe;West Asia;class;flights" | |
} | |
' | |
curl -XPOST 'http://localhost:9200/test/news/3' -d ' | |
{ "title": "Example 3", | |
"description": "While in most of the...", | |
"tags": "Europe;money;cars", | |
"tags_analyzed": "Europe;money;cars" | |
} | |
' | |
#### (3) basic search | |
curl -XGET http://localhost:9200/test/_search?pretty=true -d '{ | |
"query" : { | |
"query_string" : { | |
"fields" : [ "title","description", "tags" ], | |
"query" : "example" | |
} | |
}, | |
"facets" : { | |
"tags" : { | |
"terms" : {"field" : "tags_analyzed" } | |
} | |
} | |
} | |
' | |
---> Results: | |
{ | |
"took" : 7, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 3, | |
"max_score" : 0.058849156, | |
"hits" : [ { | |
"_index" : "test", | |
"_type" : "news", | |
"_id" : "1", | |
"_score" : 0.058849156, "_source" : | |
{ "title": "Example 1", | |
"description": "In this year...", | |
"tags": "Europe; West Asia; money; year" | |
} | |
}, { | |
"_index" : "test", | |
"_type" : "news", | |
"_id" : "2", | |
"_score" : 0.058849156, "_source" : | |
{ "title": "Example 2", | |
"description": "While in most of the...", | |
"tags": "Europe; West Asia; class; flights" | |
} | |
}, { | |
"_index" : "test", | |
"_type" : "news", | |
"_id" : "3", | |
"_score" : 0.058849156, "_source" : | |
{ "title": "Example 3", | |
"description": "While in most of the...", | |
"tags": "Europe; money; cars" | |
} | |
} ] | |
}, | |
"facets" : { | |
"tags" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 11, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "europe", | |
"count" : 3 | |
}, { | |
"term" : " west asia", | |
"count" : 2 | |
}, { | |
"term" : " money", | |
"count" : 2 | |
}, { | |
"term" : " year", | |
"count" : 1 | |
}, { | |
"term" : " flights", | |
"count" : 1 | |
}, { | |
"term" : " class", | |
"count" : 1 | |
}, { | |
"term" : " cars", | |
"count" : 1 | |
} ] | |
} | |
} | |
} | |
#### (4) search concatening all conditions in one query | |
curl -XGET http://localhost:9200/test/_search?pretty=true -d '{ | |
"query" : { | |
"query_string" : { | |
"fields" : [ "title","description", "tags" ], | |
"query" : "example AND tags:\"west asia\"" | |
} | |
}, | |
"facets" : { | |
"tags" : { | |
"terms" : {"field" : "tags_analyzed" } | |
} | |
} | |
} | |
' | |
---> Results : | |
{ | |
"took" : 3, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 0.19059631, | |
"hits" : [ { | |
"_index" : "test", | |
"_type" : "news", | |
"_id" : "1", | |
"_score" : 0.19059631, "_source" : | |
{ "title": "Example 1", | |
"description": "In this year...", | |
"tags": "Europe;West Asia;money;year", | |
"tags_an": "Europe;West Asia;money;year" | |
} | |
}, { | |
"_index" : "test", | |
"_type" : "news", | |
"_id" : "2", | |
"_score" : 0.19059631, "_source" : | |
{ "title": "Example 2", | |
"description": "While in most of the...", | |
"tags": "Europe;West Asia;class;flights", | |
"tags_an": "Europe;West Asia;class;flights" | |
} | |
} ] | |
}, | |
"facets" : { | |
"tags" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 8, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "west asia", | |
"count" : 2 | |
}, { | |
"term" : "europe", | |
"count" : 2 | |
}, { | |
"term" : "year", | |
"count" : 1 | |
}, { | |
"term" : "money", | |
"count" : 1 | |
}, { | |
"term" : "flights", | |
"count" : 1 | |
}, { | |
"term" : "class", | |
"count" : 1 | |
} ] | |
} | |
} | |
} | |
#### (5) search using filters in the not analyzed field | |
curl -XGET http://localhost:9200/test/_search?pretty=true -d '{ | |
"query" : { | |
"filtered" : { | |
"query": { | |
"query_string" : { | |
"fields" : [ "title^5","description", "tags" ], | |
"query" : "example" | |
} | |
}, | |
"filter" : { | |
"and" : [ | |
{ | |
"term" : { | |
"tags" : "west asia" | |
} | |
}, | |
{ | |
"term" : { | |
"tags" : "year" | |
} | |
} | |
]} | |
} | |
}, | |
"facets" : { | |
"tags" : { | |
"terms" : {"field" : "tags_analyzed" } | |
} | |
} | |
} | |
' | |
-> Results are null! | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 0, | |
"max_score" : null, | |
"hits" : [ ] | |
}, | |
"facets" : { | |
"tags" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 0, | |
"other" : 0, | |
"terms" : [ ] | |
} | |
} | |
} | |
#### (6) search using filters in the analyzed field (tags_analyzed) | |
curl -XGET http://localhost:9200/test/_search?pretty=true -d '{ | |
"query" : { | |
"filtered" : { | |
"query": { | |
"query_string" : { | |
"fields" : [ "title^5","description", "tags" ], | |
"query" : "example" | |
} | |
}, | |
"filter" : { | |
"and" : [ | |
{ | |
"term" : { | |
"tags_analyzed" : "west asia" | |
} | |
}, | |
{ | |
"term" : { | |
"tags_analyzed" : "year" | |
} | |
} | |
]} | |
} | |
}, | |
"facets" : { | |
"tags" : { | |
"terms" : {"field" : "tags_analyzed" } | |
} | |
} | |
} | |
' | |
--> Results are correct! | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 1, | |
"max_score" : 0.19178301, | |
"hits" : [ { | |
"_index" : "test", | |
"_type" : "news", | |
"_id" : "1", | |
"_score" : 0.19178301, "_source" : | |
{ "title": "Example 1", | |
"description": "In this year...", | |
"tags": "Europe;West Asia;money;year", | |
"tags_an": "Europe;West Asia;money;year" | |
} | |
} ] | |
}, | |
"facets" : { | |
"tags" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 4, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "year", | |
"count" : 1 | |
}, { | |
"term" : "west asia", | |
"count" : 1 | |
}, { | |
"term" : "money", | |
"count" : 1 | |
}, { | |
"term" : "europe", | |
"count" : 1 | |
} ] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment