Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wildcard & lang field matching to multimatch queries #131

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions layout/FallbackQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,23 @@ function addSecCountry(vs, o) {


function addQuery(vs) {
var fields = [
'phrase.default',
'phrase.default_*'
];

var lang = vs.var('lang').get();
if (_.isString(lang) && !_.isEmpty(lang)) {
fields.push(
`phrase.${lang}`,
`phrase.${lang}_*`
);
}

var o = addPrimary(
vs.var('input:query').toString(),
'venue',
[
'phrase.default'
],
fields,
false
);

Expand Down
18 changes: 14 additions & 4 deletions layout/StructuredFallbackQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,23 @@ function addSecCountry(vs, o) {


function addQuery(vs) {
var fields = [
'phrase.default',
'phrase.default_*'
];

var lang = vs.var('lang').get();
if (_.isString(lang) && !_.isEmpty(lang)) {
fields.push(
`phrase.${lang}`,
`phrase.${lang}_*`
);
}

var o = addPrimary(
vs.var('input:query').toString(),
'venue',
[
'phrase.default',
'category'
],
fields.concat(['category']),
false
);

Expand Down
13 changes: 8 additions & 5 deletions layout/VenuesQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ class VenuesQuery extends Query {
bool: {
must: [
{
match_phrase: {
'name.default': {
query: vs.var('input:query'),
analyzer: 'standard'
}
multi_match: {
query: vs.var('input:query'),
type: 'phrase',
analyzer: 'standard',
fields: [
'name.default',
'name.default_*'
]
}
}
],
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/fallbackQuery1-with-lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const _ = require('lodash');
const query = _.cloneDeep(require('./fallbackQuery1.json'));
const jpath = 'query.function_score.query.bool.should[0].bool.must[0].multi_match.fields';

_.set(query, jpath, _.get(query, jpath).concat(
'phrase.foo',
'phrase.foo_*'
));

module.exports = query;
3 changes: 2 additions & 1 deletion test/fixtures/fallbackQuery1.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"query": "query value",
"type": "phrase",
"fields": [
"phrase.default"
"phrase.default",
"phrase.default_*"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/structuredFallbackQuery/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"type": "phrase",
"fields": [
"phrase.default",
"phrase.default_*",
"category"
]
}
Expand Down
13 changes: 8 additions & 5 deletions test/fixtures/venuesQuery/base_render.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"bool": {
"must": [
{
"match_phrase": {
"name.default": {
"query": "query value",
"analyzer": "standard"
}
"multi_match": {
"query": "query value",
"type": "phrase",
"analyzer": "standard",
"fields": [
"name.default",
"name.default_*"
]
}
}
],
Expand Down
13 changes: 8 additions & 5 deletions test/fixtures/venuesQuery/with_filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"bool": {
"must": [
{
"match_phrase": {
"name.default": {
"query": "query value",
"analyzer": "standard"
}
"multi_match": {
"query": "query value",
"type": "phrase",
"analyzer": "standard",
"fields": [
"name.default",
"name.default_*"
]
}
}
],
Expand Down
13 changes: 8 additions & 5 deletions test/fixtures/venuesQuery/with_scores.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"bool": {
"must": [
{
"match_phrase": {
"name.default": {
"query": "query value",
"analyzer": "standard"
}
"multi_match": {
"query": "query value",
"type": "phrase",
"analyzer": "standard",
"fields": [
"name.default",
"name.default_*"
]
}
}
],
Expand Down
30 changes: 30 additions & 0 deletions test/layout/FallbackQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ module.exports.tests.base_render = function(test, common) {

});

test('VariableStore with query AND street should only add query - with lang', function (t) {
var query = new FallbackQuery();

var vs = new VariableStore();
vs.var('size', 'size value');
vs.var('track_scores', 'track_scores value');
vs.var('input:query', 'query value');
vs.var('input:unit', 'unit value');
vs.var('input:housenumber', 'house number value');
vs.var('input:street', 'street value');
vs.var('input:neighbourhood', 'neighbourhood value');
vs.var('input:borough', 'borough value');
vs.var('input:locality', 'locality value');
vs.var('input:county', 'county value');
vs.var('input:region', 'region value');
vs.var('input:country', 'country value');
vs.var('boost:address', 19);
vs.var('boost:street', 17);

// everything is same as above except lang is set here
vs.var('lang', 'foo');

var actual = JSON.parse(JSON.stringify(query.render(vs)));
var expected = require('../fixtures/fallbackQuery1-with-lang.js');

t.deepEquals(actual, expected);
t.end();

});

test('VariableStore with number+street and less granular fields should include all others', function(t) {
var query = new FallbackQuery();

Expand Down
63 changes: 35 additions & 28 deletions test/view/phrase.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var phrase = require('../../view/phrase');
var VariableStore = require('../../lib/VariableStore');
const _ = require('lodash');
const phrase = require('../../view/phrase');
const VariableStore = require('../../lib/VariableStore');

function getBaseVariableStore(toExclude) {
var vs = new VariableStore();
Expand Down Expand Up @@ -48,14 +49,16 @@ module.exports.tests.no_exceptions_conditions = function(test, common) {
var actual = phrase(getBaseVariableStore());

var expected = {
match: {
'field value': {
analyzer: { $: 'analyzer value' },
type: 'phrase',
boost: { $: 'boost value' },
slop: { $: 'slop value' },
query: { $: 'name value' }
}
multi_match: {
analyzer: { $: 'analyzer value' },
type: 'phrase',
boost: { $: 'boost value' },
slop: { $: 'slop value' },
query: { $: 'name value' },
fields: [
'field value',
'field value_*'
]
}
};

Expand All @@ -74,15 +77,17 @@ module.exports.tests.fuzziness_variable = function(test, common) {
var actual = phrase(store);

var expected = {
match: {
'field value': {
analyzer: { $: 'analyzer value' },
type: 'phrase',
boost: { $: 'boost value' },
slop: { $: 'slop value' },
query: { $: 'name value' },
fuzziness: { $: 'fuzziness value' }
}
multi_match: {
analyzer: { $: 'analyzer value' },
type: 'phrase',
boost: { $: 'boost value' },
slop: { $: 'slop value' },
query: { $: 'name value' },
fuzziness: { $: 'fuzziness value' },
fields: [
'field value',
'field value_*'
]
}
};

Expand All @@ -100,15 +105,17 @@ module.exports.tests.cutoff_frequency = function(test, common) {
var actual = phrase(store);

var expected = {
match: {
'field value': {
analyzer: { $: 'analyzer value' },
type: 'phrase',
boost: { $: 'boost value' },
slop: { $: 'slop value' },
query: { $: 'name value' },
cutoff_frequency: { $: 'cutoff_frequency value' }
}
multi_match: {
analyzer: { $: 'analyzer value' },
type: 'phrase',
boost: { $: 'boost value' },
slop: { $: 'slop value' },
query: { $: 'name value' },
cutoff_frequency: { $: 'cutoff_frequency value' },
fields: [
'field value',
'field value_*'
]
}
};

Expand Down
21 changes: 16 additions & 5 deletions view/phrase.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');

module.exports = function( vs ){

Expand All @@ -11,23 +12,33 @@ module.exports = function( vs ){
}

// base view
var view = { 'match': {} };
var view = { 'multi_match': {} };

var pf = vs.var('phrase:field').get();
var fields = [pf, `${pf}_*`];

var lang = vs.var('lang').get();
if (_.isString(lang) && !_.isEmpty(lang)) {
var lf = pf.replace('default', lang);
fields.push(lf, `${lf}_*`);
}

// match query
view.match[ vs.var('phrase:field') ] = {
view.multi_match = {
analyzer: vs.var('phrase:analyzer'),
type: 'phrase',
boost: vs.var('phrase:boost'),
slop: vs.var('phrase:slop'),
query: vs.var('input:name')
query: vs.var('input:name'),
fields: fields
};

if (vs.isset('phrase:fuzziness')) {
view.match[ vs.var('phrase:field') ].fuzziness = vs.var('phrase:fuzziness');
view.multi_match.fuzziness = vs.var('phrase:fuzziness');
}

if (vs.isset('phrase:cutoff_frequency')) {
view.match[ vs.var('phrase:field') ].cutoff_frequency = vs.var('phrase:cutoff_frequency');
view.multi_match.cutoff_frequency = vs.var('phrase:cutoff_frequency');
}

return view;
Expand Down