Skip to content

Instantly share code, notes, and snippets.

@nickhoffman
Created January 31, 2012 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickhoffman/df6321bdd0b6b5d599f8 to your computer and use it in GitHub Desktop.
Save nickhoffman/df6321bdd0b6b5d599f8 to your computer and use it in GitHub Desktop.
Is this query ideal for a generic search?
See this mailing list post for details:
https://groups.google.com/forum/#!topic/elasticsearch/V7Ly0eH-qkM
The 1st document listed below has 1 occurrence of "Grimlock".
The 2nd document lsited below has 7 occurrences of "Grimlock".
Despite this, the 1st document is scored higher than the 2nd document.
How can that be, considering that the 1st document matches on 1 field that's boosted by 4,
whereas the 2nd document matches on 1 field that's boosted by 4, and 6 fields that're boosted by 2?
The last file in this gist is the query's explanation.
// This is the query that I'm trying to improve.
{
"from":0,
"size":10,
"query":{
"dis_max":{
"queries":[
{ "field":{ "name":"Grimlock" } },
{ "field":{ "catalog.name":"Grimlock" } },
{ "field":{ "items.name":"Grimlock" } },
{ "field":{ "items.property_attribs.character.analyzed":"Grimlock" } },
{ "field":{ "_all":"Grimlock" } }
]
}
}
}
// This is the product mapping.
{
"product": {
"dynamic_templates": [
{
"string_property": {
"match_mapping_type": "string",
"path_match" : "*property_attribs.*",
"mapping" : {
"type" : "multi_field",
"fields": {
"{name}": {
"type" : "{dynamic_type}",
"index" : "analyzed",
"include_in_all": true
},
"orig": {
"type" : "{dynamic_type}",
"index" : "not_analyzed",
"include_in_all": false
}
}
}
}
}
],
"properties": {
"id": {
"type": "string",
"index": "not_analyzed",
"include_in_all": false
},
"name": {
"type" : "multi_field",
"fields": {
"name": {
"type" : "string",
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std",
"boost" : 4.0,
"include_in_all": false
},
"orig": {
"type" : "string",
"index" : "not_analyzed",
"include_in_all": false
}
}
},
"number": {
"type" : "string",
"index" : "not_analyzed",
"include_in_all": false
},
"property_attribs": {
"properties": {
"product_packaging": {
"type" : "multi_field",
"fields": {
"product_packaging": {
"type" : "string",
"analyzer" : "ascii_std",
"include_in_all": false
},
"orig": {
"type" : "string",
"index" : "not_analyzed",
"include_in_all": false
}
}
},
"release_date": {
"type" : "date",
"format" : "YYYY-MM-DD",
"include_in_all": true
}
}
},
"catalog": {
"properties": {
"id": {
"type" : "string",
"index" : "not_analyzed",
"include_in_all": false
},
"name": {
"type" : "string",
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std",
"include_in_all": false
},
"brand": {
"type" : "string",
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std",
"include_in_all": true
}
}
},
"items": {
"properties": {
"id": {
"type" : "string",
"index" : "not_analyzed",
"include_in_all": false
},
"name": {
"type" : "string",
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std",
"boost" : 2.0,
"include_in_all": false
},
"property_attribs": {
"properties": {
"character": {
"type" : "multi_field",
"fields": {
"character": {
"type" : "string",
"index_analyzer" : "ascii_edge_ngram",
"search_analyzer" : "ascii_std",
"boost" : 2.0,
"include_in_all": true
},
"orig": {
"type" : "string",
"index" : "not_analyzed",
"include_in_all": false
}
}
}
}
}
}
},
"_all": {
"type" : "string",
"analyzer": "ascii_std"
}
}
}
}
// This is a sample product document.
{
"_type":"product",
"id":"4ef12838349c300a8c0005da",
"name":"Cyber Stompin' Optimus Prime",
"number":"TF-TM-171",
"property_attribs":{
"msrp__usa_":1999,
"msrp__canada_":null,
"release_date":"2007-06-01",
"product_packaging":"MISB",
"transformers_manufacturer":"Hasbro",
"transformers_product_class":"Cyber Stompin'",
"exclusive":null,
"sub_series":"Cyber Stompin'",
"product_line":"Transformers (2007)",
"series":null
},
"catalog":{
"_type":"catalog",
"id":"4ee971eb55d42a503e000025",
"name":"The Movie",
"brand":"Transformers",
"property_attribs":{
}
},
"items":[
{
"_type":"product_item",
"id":"4ef1284c349c300a8c0005ec",
"name":"Cyber Stompin' Optimus Prime (Cullen)",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
},
{
"_type":"product_item",
"id":"4ef1285c349c300a8c0005f9",
"name":"Fist",
"item_type":"Part",
"property_attribs":{
}
},
{
"_type":"product_item",
"id":"4ef12867349c300a8c000602",
"name":"Instructions",
"item_type":"Paperwork",
"property_attribs":{
}
},
{
"_type":"product_item",
"id":"4ef12899349c300a8c000607",
"name":"Cyber Stompin' Optimus Prime (Chalk)",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
},
{
"_type":"product_item",
"id":"4ef25898349c300a8c001018",
"name":"Cyber Stompin' Optimus Prime + Cyber Slammer Bumblebee",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
},
{
"_type":"product_item",
"id":"4ef258fb349c300a8c00101d",
"name":"Cyber Stompin' Optimus Prime + Robot Heroes Ravage & Prime",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
},
{
"_type":"product_item",
"id":"4ef25953349c300a8c001023",
"name":"Cyber Stompin' Optimus Prime + Robot Heroes Rodimus & Insecticon",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
},
{
"_type":"product_item",
"id":"4ef2597b349c300a8c001028",
"name":"Cyber Stompin' Optimus Prime + Robot Heroes Shockwave & Grimlock",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
},
{
"_type":"product_item",
"id":"4ef2599e349c300a8c00102d",
"name":"Cyber Stompin' Optimus Prime + Robot Heroes Starscream & Mirage",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Cyber Stompin'",
"transformers_item_allegiance":"Autobot",
"character":"Optimus Prime",
"transformers_item_alternate_modes":"N/A"
}
}
]
}
{
"_type":"product",
"id":"4ee26242349c30724100072c",
"name":"Grimlock",
"number":"TF-G2-38",
"property_attribs":{
"msrp__usa_":1599,
"msrp__canada_":null,
"release_date":"1993-01-01",
"product_packaging":"MOSC",
"transformers_manufacturer":"Hasbro",
"exclusive":null,
"sub_series":"Dinobots",
"product_line":"Generation 2",
"series":null
},
"catalog":{
"_type":"catalog",
"id":"4ee0c06255d42a79ae00039d",
"name":"Generation 2",
"brand":"Transformers",
"property_attribs":{
}
},
"items":[
{
"_type":"product_item",
"id":"4ee2627d349c30724100073d",
"name":"Grimlock (G1)",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Mega",
"transformers_item_allegiance":"Dinobot",
"character":"Grimlock",
"transformers_item_alternate_modes":"Tyrannosaurus Rex"
}
},
{
"_type":"product_item",
"id":"4ee262a3349c307241000749",
"name":"Grimlock (Blue)",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Mega",
"transformers_item_allegiance":"Dinobot",
"character":"Grimlock",
"transformers_item_alternate_modes":"Tyrannosaurus Rex"
}
},
{
"_type":"product_item",
"id":"4ee262b4349c30724100074e",
"name":"Grimlock (Turquoise)",
"item_type":"Transformer",
"property_attribs":{
"transformers_item_class":"Mega",
"transformers_item_allegiance":"Dinobot",
"character":"Grimlock",
"transformers_item_alternate_modes":"Tyrannosaurus Rex"
}
},
{
"_type":"product_item",
"id":"4ee262d0349c307241000757",
"name":"Energo Sword",
"item_type":"Part",
"property_attribs":{
}
},
{
"_type":"product_item",
"id":"4ee262ff349c307241000760",
"name":"Twin Laser",
"item_type":"Part",
"property_attribs":{
}
},
{
"_type":"product_item",
"id":"4ee26314349c307241000769",
"name":"Sticker Sheet",
"item_type":"Part",
"property_attribs":{
}
},
{
"_type":"product_item",
"id":"4ee26322349c307241000772",
"name":"Instructions",
"item_type":"Part",
"property_attribs":{
}
}
]
}
// curl 'localhost:9200/development_products/product/_search?pretty=1&explain=1' -d '
{
"from":3,
"size":2,
"fields": ["name"],
"query": {
"dis_max": {
"queries": [
{"field":{"name":"Grimlock"}},
{"field":{"items.name":"Grimlock"}},
{"field":{"items.property_attribs.character.analyzed":"Grimlock"}}
]
}
}
}
//'
{
"took":8,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":24,
"max_score":3.02617984E8,
"hits":[
{
"_shard":0,
"_node":"4-lxi8wUSWuOGNdUmJXXww",
"_index":"development_products",
"_type":"product",
"_id":"4ef12838349c300a8c0005da",
"_score":138.59901,
"fields":{
"name":"Cyber Stompin' Optimus Prime"
},
"_explanation":{
"value":138.59901,
"description":"max of:",
"details":[
{
"value":138.59901,
"description":"weight(items.name:grimlock in 424), product of:",
"details":[
{
"value":0.7483759,
"description":"queryWeight(items.name:grimlock), product of:",
"details":[
{
"value":5.787492,
"description":"idf(docFreq=6, maxDocs=840)"
},
{
"value":0.12930919,
"description":"queryNorm"
}
]
},
{
"value":185.19974,
"description":"fieldWeight(items.name:grimlock in 424), product of:",
"details":[
{
"value":1.0,
"description":"tf(termFreq(items.name:grimlock)=1)"
},
{
"value":5.787492,
"description":"idf(docFreq=6, maxDocs=840)"
},
{
"value":32.0,
"description":"fieldNorm(field=items.name, doc=424)"
}
]
}
]
}
]
}
},
{
"_shard":4,
"_node":"4-lxi8wUSWuOGNdUmJXXww",
"_index":"development_products",
"_type":"product",
"_id":"4ee26242349c30724100072c",
"_score":124.96703,
"fields":{
"name":"Grimlock"
},
"_explanation":{
"value":124.96703,
"description":"max of:",
"details":[
{
"value":7.189802,
"description":"weight(name:grimlock in 440), product of:",
"details":[
{
"value":0.7902892,
"description":"queryWeight(name:grimlock), product of:",
"details":[
{
"value":6.0651236,
"description":"idf(docFreq=4, maxDocs=792)"
},
{
"value":0.1303006,
"description":"queryNorm"
}
]
},
{
"value":9.097685,
"description":"fieldWeight(name:grimlock in 440), product of:",
"details":[
{
"value":1.0,
"description":"tf(termFreq(name:grimlock)=1)"
},
{
"value":6.0651236,
"description":"idf(docFreq=4, maxDocs=792)"
},
{
"value":1.5,
"description":"fieldNorm(field=name, doc=440)"
}
]
}
]
},
{
"value":124.96703,
"description":"weight(items.name:grimlock in 440), product of:",
"details":[
{
"value":0.7665326,
"description":"queryWeight(items.name:grimlock), product of:",
"details":[
{
"value":5.882802,
"description":"idf(docFreq=5, maxDocs=792)"
},
{
"value":0.1303006,
"description":"queryNorm"
}
]
},
{
"value":163.02899,
"description":"fieldWeight(items.name:grimlock in 440), product of:",
"details":[
{
"value":1.7320508,
"description":"tf(termFreq(items.name:grimlock)=3)"
},
{
"value":5.882802,
"description":"idf(docFreq=5, maxDocs=792)"
},
{
"value":16.0,
"description":"fieldNorm(field=items.name, doc=440)"
}
]
}
]
}
]
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment