I have a query that applies my function score with 1 input but doesnt apply it with another input. Is this a bug or am I missing something?
Input: “cambridg”
{
compound: {
must: [
{
compound: {
should: [
{
text: {
path: 'type',
query: 'COUNTRY',
score: { constant: { value: 90000 } }
}
},
{
compound: {
filter: [
{ text: { path: 'type', query: 'POI' } },
{ text: { path: 'poiType', query: 'ISLAND' } },
{ equals: { path: 'hasGeometry', value: true } }
],
score: { constant: { value: 80000 } }
}
},
{
compound: {
filter: [
{ text: { path: 'type', query: 'CITY' } },
{ exists: { path: 'accommodationCount' } },
{ range: { path: 'accommodationCount', gt: 0 } }
],
score: { constant: { value: 70000 } }
}
},
{
text: {
path: 'type',
query: 'ADMINISTRATIVE_LEVEL',
score: { constant: { value: 60000 } }
}
},
{
text: {
path: 'type',
query: 'CONTINENT',
score: { constant: { value: 50000 } }
}
},
{
compound: {
filter: [
{ text: { path: 'type', query: 'POI' } },
{
compound: {
should: [
{
text: { path: 'poiType', query: 'ATTRACTION' }
},
{
text: { path: 'poiType', query: 'TRANSPORTATION' }
}
],
minimumShouldMatch: 1
}
}
],
score: { constant: { value: 40000 } }
}
},
{
compound: {
filter: [ { text: { path: 'type', query: 'CITY' } } ],
mustNot: [
{ exists: { path: 'accommodationCount' } },
{ range: { path: 'accommodationCount', gt: 0 } }
],
score: { constant: { value: 30000 } }
}
},
{
compound: {
filter: [ { text: { path: 'type', query: 'POI' } } ],
mustNot: [
{
compound: {
should: [
{
compound: {
filter: [
{
text: { path: 'poiType', query: 'ISLAND' }
},
{
equals: { path: 'hasGeometry', value: true }
}
]
}
},
{
text: { path: 'poiType', query: 'ATTRACTION' }
},
{
text: { path: 'poiType', query: 'TRANSPORTATION' }
}
],
minimumShouldMatch: 1
}
}
],
score: { constant: { value: 20000 } }
}
},
{
text: {
path: 'type',
query: 'CITY_DISTRICT',
score: { constant: { value: 10000 } }
}
}
]
}
}
],
filter: [
{
compound: {
should: [
{ text: { path: 'type', query: 'POI' } },
{
compound: {
should: [
{ text: { path: 'type', query: 'COUNTRY' } },
{
text: { path: 'type', query: 'ADMINISTRATIVE_LEVEL' }
},
{ text: { path: 'type', query: 'CITY' } }
],
filter: [ { range: { path: 'accommodationCount', gt: 0 } } ],
minimumShouldMatch: 1
}
}
],
minimumShouldMatch: 1
}
}
],
should: [
{
text: {
query: 'cambridge',
path: [
{ value: 'names', multi: 'keyword' },
{ value: 'alternateAndParentNames', multi: 'keyword' }
],
score: { constant: { value: 2000000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [
{ value: 'names', multi: 'keyword' },
{ value: 'alternateAndParentNames', multi: 'keyword' }
],
score: { constant: { value: 10000000 } }
}
},
{
text: {
query: 'cambridge',
path: [ { value: 'names' }, { value: 'alternateAndParentNames' } ],
score: { constant: { value: 2000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [ { value: 'names' }, { value: 'alternateAndParentNames' } ],
score: { constant: { value: 10000 } }
}
},
{
text: {
query: 'cambridge',
path: [ { value: 'names', multi: 'keyword' } ],
score: { constant: { value: 20000000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [ { value: 'names', multi: 'keyword' } ],
score: { constant: { value: 100000000 } }
}
},
{
text: {
query: 'cambridge',
path: [ { value: 'names' } ],
score: { constant: { value: 20000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [ { value: 'names' } ],
score: { constant: { value: 100000 } }
}
}
],
minimumShouldMatch: 1,
score: {
function: {
add: [
{ score: 'relevance' },
{ path: { value: 'accommodationCount', undefined: 0 } },
{ path: { value: 'searchBoost', undefined: 0 } }
]
}
}
}
}
Gives output:
[
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'England', 'Cambridgeshire' ],
countryName: 'United Kingdom of Great Britain and Northern Ireland'
},
searchScoreWithBoost: 110180264,
searchScoreWithoutBoost: 110179997,
accommodationCount: 267,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Waikato' ],
countryName: 'New Zealand'
},
searchScoreWithBoost: 110180064,
searchScoreWithoutBoost: 110180001,
accommodationCount: 63,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Massachusetts' ],
countryName: 'United States'
},
searchScoreWithBoost: 110180008,
searchScoreWithoutBoost: 110179999,
accommodationCount: 9,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Ontario' ],
countryName: 'Canada'
},
searchScoreWithBoost: 110180008,
searchScoreWithoutBoost: 110180003,
accommodationCount: 5,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Maryland' ],
countryName: 'United States'
},
searchScoreWithBoost: 110180000,
searchScoreWithoutBoost: 110179996,
accommodationCount: 4,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge City',
administrativeLevelNames: [ 'Indiana' ],
countryName: 'United States'
},
searchScoreWithBoost: 110180000,
searchScoreWithoutBoost: 110179999,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'State of Tasmania' ],
countryName: 'Commonwealth of Australia'
},
searchScoreWithBoost: 110180000,
searchScoreWithoutBoost: 110179999,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'England', 'Gloucestershire' ],
countryName: 'United Kingdom of Great Britain and Northern Ireland'
},
searchScoreWithBoost: 110180000,
searchScoreWithoutBoost: 110179999,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Wisconsin' ],
countryName: 'United States'
},
searchScoreWithBoost: 110180000,
searchScoreWithoutBoost: 110179999,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Vermont' ],
countryName: 'United States'
},
searchScoreWithBoost: 110180000,
searchScoreWithoutBoost: 110179999,
accommodationCount: 1,
searchBoost: undefined
}
]
Input: “cambridge”
{
compound: {
must: [
{
compound: {
should: [
{
text: {
path: 'type',
query: 'COUNTRY',
score: { constant: { value: 90000 } }
}
},
{
compound: {
filter: [
{ text: { path: 'type', query: 'POI' } },
{ text: { path: 'poiType', query: 'ISLAND' } },
{ equals: { path: 'hasGeometry', value: true } }
],
score: { constant: { value: 80000 } }
}
},
{
compound: {
filter: [
{ text: { path: 'type', query: 'CITY' } },
{ exists: { path: 'accommodationCount' } },
{ range: { path: 'accommodationCount', gt: 0 } }
],
score: { constant: { value: 70000 } }
}
},
{
text: {
path: 'type',
query: 'ADMINISTRATIVE_LEVEL',
score: { constant: { value: 60000 } }
}
},
{
text: {
path: 'type',
query: 'CONTINENT',
score: { constant: { value: 50000 } }
}
},
{
compound: {
filter: [
{ text: { path: 'type', query: 'POI' } },
{
compound: {
should: [
{
text: { path: 'poiType', query: 'ATTRACTION' }
},
{
text: { path: 'poiType', query: 'TRANSPORTATION' }
}
],
minimumShouldMatch: 1
}
}
],
score: { constant: { value: 40000 } }
}
},
{
compound: {
filter: [ { text: { path: 'type', query: 'CITY' } } ],
mustNot: [
{ exists: { path: 'accommodationCount' } },
{ range: { path: 'accommodationCount', gt: 0 } }
],
score: { constant: { value: 30000 } }
}
},
{
compound: {
filter: [ { text: { path: 'type', query: 'POI' } } ],
mustNot: [
{
compound: {
should: [
{
compound: {
filter: [
{
text: { path: 'poiType', query: 'ISLAND' }
},
{
equals: { path: 'hasGeometry', value: true }
}
]
}
},
{
text: { path: 'poiType', query: 'ATTRACTION' }
},
{
text: { path: 'poiType', query: 'TRANSPORTATION' }
}
],
minimumShouldMatch: 1
}
}
],
score: { constant: { value: 20000 } }
}
},
{
text: {
path: 'type',
query: 'CITY_DISTRICT',
score: { constant: { value: 10000 } }
}
}
]
}
}
],
filter: [
{
compound: {
should: [
{ text: { path: 'type', query: 'POI' } },
{
compound: {
should: [
{ text: { path: 'type', query: 'COUNTRY' } },
{
text: { path: 'type', query: 'ADMINISTRATIVE_LEVEL' }
},
{ text: { path: 'type', query: 'CITY' } }
],
filter: [ { range: { path: 'accommodationCount', gt: 0 } } ],
minimumShouldMatch: 1
}
}
],
minimumShouldMatch: 1
}
}
],
should: [
{
text: {
query: 'cambridge',
path: [
{ value: 'names', multi: 'keyword' },
{ value: 'alternateAndParentNames', multi: 'keyword' }
],
score: { constant: { value: 2000000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [
{ value: 'names', multi: 'keyword' },
{ value: 'alternateAndParentNames', multi: 'keyword' }
],
score: { constant: { value: 10000000 } }
}
},
{
text: {
query: 'cambridge',
path: [ { value: 'names' }, { value: 'alternateAndParentNames' } ],
score: { constant: { value: 2000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [ { value: 'names' }, { value: 'alternateAndParentNames' } ],
score: { constant: { value: 10000 } }
}
},
{
text: {
query: 'cambridge',
path: [ { value: 'names', multi: 'keyword' } ],
score: { constant: { value: 20000000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [ { value: 'names', multi: 'keyword' } ],
score: { constant: { value: 100000000 } }
}
},
{
text: {
query: 'cambridge',
path: [ { value: 'names' } ],
score: { constant: { value: 20000000 } }
}
},
{
wildcard: {
query: 'cambridge*',
allowAnalyzedField: true,
path: [ { value: 'names' } ],
score: { constant: { value: 100000 } }
}
}
],
minimumShouldMatch: 1,
score: {
function: {
add: [
{ score: 'relevance' },
{ path: { value: 'accommodationCount', undefined: 0 } },
{ path: { value: 'searchBoost', undefined: 0 } }
]
}
}
}
}
Gives output:
[
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Maryland' ],
countryName: 'United States'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178940,
accommodationCount: 4,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Waikato' ],
countryName: 'New Zealand'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178881,
accommodationCount: 63,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Massachusetts' ],
countryName: 'United States'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178935,
accommodationCount: 9,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'State of Tasmania' ],
countryName: 'Commonwealth of Australia'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178943,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'England', 'Gloucestershire' ],
countryName: 'United Kingdom of Great Britain and Northern Ireland'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178943,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Vermont' ],
countryName: 'United States'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178943,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Wisconsin' ],
countryName: 'United States'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178943,
accommodationCount: 1,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Ontario' ],
countryName: 'Canada'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178939,
accommodationCount: 5,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'England', 'Cambridgeshire' ],
countryName: 'United Kingdom of Great Britain and Northern Ireland'
},
searchScoreWithBoost: 22132178944,
searchScoreWithoutBoost: 22132178677,
accommodationCount: 267,
searchBoost: undefined
},
{
i18n: {
name: 'Cambridge',
administrativeLevelNames: [ 'Eastern Cape' ],
countryName: 'South Africa'
},
searchScoreWithBoost: 22132150272,
searchScoreWithoutBoost: 22132150272,
accommodationCount: undefined,
searchBoost: undefined
}
]
The main difference is that “cambridge” is hitting exact keyword matches.
The problem is that “cambridg” is adding the function score (bottom of query, “score:”) to the score result and thus orders the results correctly. For “cambridge” it doensn’t do this and the scores are all the same (as shown by the searchScoreWithBoost property). I would have the same result with leaving out the function score.
So why does the function score not get applied in this case?