Skip to content

Instantly share code, notes, and snippets.

@q42jaap
Last active December 18, 2015 02:39
Show Gist options
  • Save q42jaap/5712231 to your computer and use it in GitHub Desktop.
Save q42jaap/5712231 to your computer and use it in GitHub Desktop.
Reproduction of sorting on child property (Note that you need 0.90.1)
#!/bin/sh
# assume the index products doesn't exist
# otherwise uncomment the following line
# curl -XDELETE localhost:9200/products
curl -XPUT localhost:9200/products/product/1 -d'{
"property1": "value1"
}'
curl -XPUT localhost:9200/products/product/2 -d'{
"property1": "value2"
}'
curl -XPOST localhost:9200/products/offer/_mapping -d '{
"book":{
"_parent": {"type": "product"}
}
}'
curl -XPOST localhost:9200/products/offer/1?parent=1 -d '{
"color": "blue",
"size": 1,
"price": 99.4
}'
curl -XPOST localhost:9200/products/offer/2?parent=1 -d '{
"color": "red",
"size": 2,
"price": 100.5
}'
curl -XPOST localhost:9200/products/offer/3?parent=2 -d '{
"color": "blue",
"size": 3,
"price": 100.7
}'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "products",
"_type" : "product",
"_id" : "1",
"_score" : 1.0, "_source" : {
"property1": "value1"
}
}, {
"_index" : "products",
"_type" : "product",
"_id" : "2",
"_score" : 1.0, "_source" : {
"property1": "value2"
}
} ]
},
"facets" : {
"size" : {
"_type" : "terms",
"missing" : 0,
"total" : 2,
"other" : 0,
"terms" : [ {
"term" : 3,
"count" : 1
}, {
"term" : 1,
"count" : 1
} ]
}
}
}
#!/bin/sh
curl -s -XPOST 'localhost:9200/products/product,offer/_search?pretty=true' -d '{
"query" : {
"has_child" : {
"type" : "offer",
"score_mode" : "max",
"query" : {
"custom_score" : {
"script" : "-doc['\''price'\''].value",
"query": {
"term" : {
"color" : "blue"
}
}
}
}
}
},
"facets" : {
"size" : {
"terms" : {
"field" : "size"
},
"global" : true,
"facet_filter" : {
"bool" : {
"must" : [
{ "term" : { "color" : "blue" } }
]
}
}
}
}
}'
@martijnvg
Copy link

The following query should work:

curl -s -XPOST 'localhost:9200/products/product,offer/_search?pretty=true' -d '{
  "query" : {
    "has_child" : {
      "type" : "offer",
      "score_mode" : "avg",
      "query" : {
        "custom_score" : {
          "script" : "doc[\"price\"].value",
          "query": {
            "term" : {
              "color" : "blue"        
            }
          }
        }
      }
    }
  },
  "facets" : {
      "size" : {
          "terms" : {
              "field" : "size"
          },
          "global" : true,
          "facet_filter" : {
            "bool" : {
              "must" : [
                { "term" : { "color" : "blue" } }
              ]
            }
          }
      }
  }
}'

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