Created
June 24, 2011 15:55
-
-
Save kimchy/1045078 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| In order to satisfy the requirements, the first thing we are going to do is create an index, and have mappings associated with the `properties` type: | |
| curl -XPUT localhost:9200/foo -d '{ | |
| "settings" : { | |
| "analysis" : { | |
| "analyzer" : { | |
| "stem" : { | |
| "tokenizer" : "standard", | |
| "filter" : ["standard", "lowercase", "stop", "porter_stem"] | |
| } | |
| } | |
| } | |
| }, | |
| "mappings" : { | |
| "properties" : { | |
| "dynamic" : true, | |
| "properties" : { | |
| "property_id" : { | |
| "type" : "string", | |
| "index" : "not_analyzed" | |
| }, | |
| "account_id" : { | |
| "type" : "string", | |
| "index" : "not_analyzed" | |
| }, | |
| "location" : { | |
| "type" : "geo_point" | |
| }, | |
| "address" : { | |
| "type" : "multi_field", | |
| "fields" : { | |
| "address" : { | |
| "type" : "string" | |
| }, | |
| "stem" : { | |
| "type" : "string", | |
| "analyzer" : "stem" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }' | |
| A few notes on the mapping: | |
| 1. Since the account_id and property_id are "keys", they should not be tokenized, so they are configured as "not_analyzed". | |
| 2. Moved the geo point location information into a separate JSON object called `location`. | |
| 3. Added stemming for the address field, and use multi field to have a standard (non stemmed) version of it (address), and a stemmed version (address.stem). | |
| Now, we can index some data: | |
| curl -XPUT localhost:9200/foo/properties/DBBA23A4-87C3-4F15-80BB-8E90C4E94C36 -d '{ | |
| "address":"723 Friendly Rd", | |
| "city":"eden", | |
| "state":"NC", | |
| "zipcode":"27288", | |
| "location" : { | |
| "lat":"36.522164", | |
| "lon":"-79.741393" | |
| }, | |
| "account_id":"4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3", | |
| "property_id":"DBBA23A4-87C3-4F15-80BB-8E90C4E94C36" | |
| }' | |
| curl -XPUT localhost:9200/foo/properties/DBBA23A4-87C3-4F15-80BB-8E90C4E94C37 -d '{ | |
| "address":"201 Country Club Dr.", | |
| "city":"eden", | |
| "state":"NC", | |
| "zipcode":"27288", | |
| "location" : { | |
| "lat":"36.506618", | |
| "lon":"-79.729823" | |
| }, | |
| "account_id":"4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3", | |
| "property_id":"DBBA23A4-87C3-4F15-80BB-8E90C4E94C37" | |
| }' | |
| curl -XPUT localhost:9200/foo/properties/DBBA23A4-87C3-4F15-80BB-8E90C4E94C38 -d '{ | |
| "address":"210 Country Club Dr.", | |
| "city":"eden", | |
| "state":"NC", | |
| "zipcode":"27288", | |
| "location" : { | |
| "lat":"36.507247", | |
| "lon":"-79.729640" | |
| }, | |
| "account_id":"4AB6F6CE-B7D3-499E-AA2C-11111111111", | |
| "property_id":"DBBA23A4-87C3-4F15-80BB-8E90C4E94C38" | |
| }' | |
| curl -XPUT localhost:9200/foo/properties/DBBA23A4-87C3-4F15-80BB-8E90C4E94C39 -d '{ | |
| "address":"730 Chatham ct.", | |
| "city":"eden", | |
| "state":"NC", | |
| "zipcode":"27288", | |
| "location" : { | |
| "lat":"36.520520", | |
| "lon":"-79.749283" | |
| }, | |
| "account_id":"4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3", | |
| "property_id":"DBBA23A4-87C3-4F15-80BB-8E90C4E94C39" | |
| }' | |
| curl -XPUT localhost:9200/foo/properties/DBBA23A4-87C3-4F15-80BB-8E90C4E94C40 -d '{ | |
| "address":"2200 Colony Club Dr", | |
| "city":"Lakeland", | |
| "state":"FL", | |
| "zipcode":"33813", | |
| "location" : { | |
| "lat":"27.944240", | |
| "lon":"-81.922019" | |
| }, | |
| "account_id":"4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3", | |
| "property_id":"DBBA23A4-87C3-4F15-80BB-8E90C4E94C40" | |
| }' | |
| curl -XPUT localhost:9200/foo/properties/DBBA23A4-87C3-4F15-80BB-8E90C4E94C41 -d '{ | |
| "address":"2100 Colonial Ave", | |
| "city":"Lakeland", | |
| "state":"FL", | |
| "zipcode":"33801", | |
| "location" : { | |
| "lat":"28.039430", | |
| "lon":"-81.920279" | |
| }, | |
| "account_id":"4AB6F6CE-B7D3-499E-AA2C-11111111111", | |
| "property_id":"DBBA23A4-87C3-4F15-80BB-8E90C4E94C41" | |
| }' | |
| Now, lets do a refresh so we make sure the data is searchable (there is an automatic refresh every 1 second): | |
| curl -XPOST localhost:9200/_refresh | |
| A simple count to check how many docs we have: | |
| curl -XGET localhost:9200/_count?q=* | |
| Now, we can start searching: | |
| -search address by whole word filtered by account id | |
| "country" to query address field, but filtered for only account_id 4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3 should return number 2 but not number 3: | |
| curl -XGET localhost:9200/foo/_search -d '{ | |
| "query" : { | |
| "filtered" : { | |
| "query" : { | |
| "query_string" : { | |
| "fields" : ["address^5", "address.stem"], | |
| "query" : "country" | |
| } | |
| }, | |
| "filter" : { | |
| "term" : { "account_id" : "4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3" } | |
| } | |
| } | |
| } | |
| }' | |
| -search partial address filtered by account id | |
| "colon" to query address field, but filtered for only account_id 4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3 should return number 6 but not number 5 | |
| curl -XGET localhost:9200/foo/_search -d '{ | |
| "query" : { | |
| "filtered" : { | |
| "query" : { | |
| "query_string" : { | |
| "fields" : ["address^5", "address.stem"], | |
| "query" : "colon*" | |
| } | |
| }, | |
| "filter" : { | |
| "term" : { "account_id" : "4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3" } | |
| } | |
| } | |
| } | |
| }' | |
| -return all properties within 20 mile radius of center of eden filtered by account id | |
| all properties within 20 mile radius of 36.506618, -79.729823, filtered for only account_id 4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3 would return 1, 2, 4 but not 3, 5, or 6 because 3 has the wrong account_id and 5 and 6 are too far away | |
| curl -XGET localhost:9200/foo/_search -d '{ | |
| "query" : { | |
| "constant_score" : { | |
| "filter" : { | |
| "and" : [ | |
| { | |
| "term" : { "account_id" : "4AB6F6CE-B7D3-499E-AA2C-D07DF29CACF3" } | |
| }, | |
| { | |
| "geo_distance" : { | |
| "distance" : 20, | |
| "location" : { | |
| "lat" : 36.506618, | |
| "lon" : -79.729823 | |
| } | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| } | |
| }' | |
| -search address by whole word not filtered by account id | |
| "country" to query address field should return numbers 2 and 3 | |
| curl -XGET localhost:9200/foo/_search -d '{ | |
| "query" : { | |
| "query_string" : { | |
| "fields" : ["address^5", "address.stem"], | |
| "query" : "country" | |
| } | |
| } | |
| }' | |
| -search partial address not filtered by account id | |
| "clu" to query address field should return 2, 3, and 5 | |
| curl -XGET localhost:9200/foo/_search -d '{ | |
| "query" : { | |
| "query_string" : { | |
| "fields" : ["address^5", "address.stem"], | |
| "query" : "clu*" | |
| } | |
| } | |
| }' | |
| -return all properties within 20 mile radius of center of eden not filtered by account id | |
| all properties within 20 mile radius of 36.506618, -79.729823 would return 1, 2, 3, and 4 but not 5 or 6 because they are both too far away | |
| curl -XGET localhost:9200/foo/_search -d '{ | |
| "query" : { | |
| "constant_score" : { | |
| "filter" : { | |
| "geo_distance" : { | |
| "distance" : 20, | |
| "location" : { | |
| "lat" : 36.506618, | |
| "lon" : -79.729823 | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment