Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
solr-frontend
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
a80b0757
authored
2016-02-04 13:46:08 -0600
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
foo
1 parent
a5eadaa4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
498 additions
and
124 deletions
src/scripts/SolrSearch.js
src/scripts/SolrSearch.js
View file @
a80b075
...
...
@@ -22,35 +22,111 @@ define(function(require) {
return
result
;
}
function
mergeStaticSets
(
startPtr
,
endPtr
,
obj
,
fieldName
)
{
function
arrayToMap
(
array
)
{
var
result
=
{};
_
.
each
(
array
,
function
(
value
)
{
result
[
value
]
=
true
;
});
return
result
;
}
var
set
=
arrayToMap
(
obj
);
var
ptr
=
startPtr
;
while
(
true
)
{
set
=
_
.
extend
(
set
,
arrayToMap
(
_
.
result
(
ptr
,
fieldName
)));
if
(
ptr
===
endPtr
)
{
break
;
}
ptr
=
ptr
.
__super__
.
constructor
;
}
return
_
.
keys
(
set
);
}
function
getItemKeyAccessor
(
item
)
{
return
item
.
get
(
'key'
);
}
var
SolrFacets
=
(
function
()
{
var
Facets
=
NestedModels
.
mixin
(
Backbone
.
Model
.
extend
({
initialize
:
function
(
data
,
options
)
{
this
.
url
=
function
()
{
return
options
.
search
.
url
();
};
_
.
each
(
this
.
keys
(),
_
.
bind
(
function
(
facetName
)
{
var
facet
=
this
.
get
(
facetName
);
facet
.
on
(
'item-change'
,
function
()
{
this
.
trigger
(
'item-change'
);
_
.
each
({
'item-change'
:
'item-change'
,
'change:query'
:
'child-query'
,
'change:queryMin'
:
'item-change'
,
'change:queryMax'
:
'item-change'
,
},
function
(
eventValue
,
eventKey
)
{
facet
.
on
(
eventKey
,
function
(
model
,
value
,
options
)
{
this
.
trigger
(
eventValue
,
facet
,
null
,
options
);
},
this
);
},
this
);
},
this
));
this
.
_doSuggestions
=
_
.
debounce
(
_
.
bind
(
function
(
childFacet
)
{
if
(
childFacet
.
get
(
'query'
))
{
this
.
fetch
({
data
:
this
.
toJSON
({
childFacet
:
childFacet
,
facetSuggestions
:
true
}),
childFacet
:
childFacet
,
facetSuggestions
:
true
,
merge
:
true
,
traditional
:
true
,
});
}
else
{
childFacet
.
set
(
'suggestions'
,
[]);
}
},
this
),
250
);
this
.
on
(
'child-query'
,
this
.
_doSuggestions
,
this
);
return
Facets
.
__super__
.
initialize
.
apply
(
this
,
arguments
);
},
resetSearch
:
function
()
{
resetSearch
:
function
(
options
)
{
_
.
each
(
this
.
values
(),
function
(
facet
)
{
facet
.
get
(
'items'
).
reset
();
facet
.
get
(
'items'
).
reset
(
null
,
options
);
});
},
applyFacetResults
:
function
(
data
)
{
applyFacetResults
:
function
(
data
,
options
)
{
options
=
options
||
{};
var
facetCounts
=
getField
(
data
,
'facet_counts'
);
var
facetIntervals
=
getField
(
facetCounts
,
'facet_intervals'
);
var
facetRanges
=
getField
(
facetCounts
,
'facet_ranges'
);
var
facetFields
=
getField
(
facetCounts
,
'facet_fields'
);
var
facetQueries
=
getField
(
facetCounts
,
'facet_queries'
);
var
statsFields
=
getField
(
data
.
stats
,
'stats_fields'
);
function
getFacetList
(
facetName
,
type
)
{
switch
(
type
)
{
case
'year'
:
case
'range'
:
return
facetRanges
[
facetName
]
?
facetRanges
[
facetName
].
counts
:
null
;
case
'interval'
:
return
facetIntervals
[
facetName
]
?
facetIntervals
[
facetName
].
counts
:
null
;
case
'year-field'
:
case
'field'
:
return
facetFields
[
facetName
];
}
}
_
.
each
(
this
.
keys
(),
_
.
bind
(
function
(
facetName
)
{
var
facet
=
this
.
get
(
facetName
);
var
type
=
facet
.
facetType
;
var
list
;
var
key
,
newItems
=
[];
var
suggestions
=
[];
_
.
each
(
getFacetList
(
'suggestions:'
+
facetName
,
type
),
function
(
value
,
index
)
{
if
(
index
%
2
===
0
)
{
key
=
value
;
}
else
if
(
value
>
0
)
{
suggestions
.
push
({
key
:
key
,
value
:
value
});
}
});
facet
.
set
(
'suggestions'
,
suggestions
);
if
(
!!
options
.
facetSuggestions
&&
options
.
childFacet
===
facet
)
{
return
;
}
var
key
;
var
list
=
getFacetList
(
facetName
,
type
);
var
newItems
=
[];
var
items
=
facet
.
get
(
'items'
);
items
.
invoke
(
'set'
,
'hidden'
,
true
);
var
valueOverrides
=
{};
...
...
@@ -59,12 +135,8 @@ define(function(require) {
excludeValues
[
includeValue
]
=
true
;
}
switch
(
type
)
{
case
'year'
:
case
'range'
:
list
=
facetRanges
[
facetName
].
counts
;
break
;
case
'year-field'
:
case
'field'
:
list
=
facetFields
[
facetName
];
_
.
each
(
facet
.
bins
,
function
(
includeValues
,
key
)
{
var
tag
=
facetName
+
':'
+
key
;
valueOverrides
[
key
]
=
facetQueries
[
tag
];
...
...
@@ -109,6 +181,7 @@ define(function(require) {
}
});
switch
(
type
)
{
case
'year-field'
:
case
'field'
:
_
.
each
(
facet
.
bins
,
function
(
includeValues
,
key
)
{
var
tag
=
facetName
+
':'
+
key
;
...
...
@@ -117,49 +190,85 @@ define(function(require) {
break
;
}
items
.
set
(
newItems
,
{
remove
:
false
});
if
(
facet
.
facetStats
)
{
var
thisFacetStats
=
statsFields
[
facetName
];
facet
.
set
({
minValue
:
thisFacetStats
.
min
,
maxValue
:
thisFacetStats
.
max
});
}
},
this
));
},
getFacetFormData
:
function
()
{
getFacetFormData
:
function
(
options
)
{
options
=
options
||
{};
var
result
=
{
facet
:
true
,
stats
:
true
,
'facet.missing'
:
true
,
'facet.field'
:
[],
'facet.range'
:
[],
'facet.interval'
:
[],
'facet.query'
:
[],
'stats.field'
:
[],
fq
:
[],
};
_
.
each
(
this
.
keys
(),
_
.
bind
(
function
(
facetName
)
{
var
facet
=
this
.
get
(
facetName
);
var
queryField
=
facet
.
queryField
?
facet
.
queryField
:
facetName
;
var
queryField
=
facet
.
queryField
;
var
facetField
=
facet
.
facetField
?
facet
.
facetField
:
queryField
;
var
type
=
facet
.
facetType
;
var
valueFormatter
;
var
facetOptions
=
{};
var
quoteFormatter
=
function
(
value
)
{
return
value
.
replace
?
(
'"'
+
value
.
replace
(
/"/
,
'\\"'
)
+
'"'
)
:
value
;
};
var
rangeFormatter
=
function
(
from
,
to
)
{
return
'['
+
quoteFormatter
(
from
)
+
' TO '
+
quoteFormatter
(
to
)
+
']'
;
};
switch
(
type
)
{
case
'year'
:
type
=
'range'
;
quoteFormatter
=
function
(
value
)
{
return
value
.
toISOString
();
};
valueFormatter
=
function
(
item
)
{
var
key
=
item
.
get
(
'key'
);
var
fromDate
,
toDate
;
if
(
key
===
'before'
)
{
return
'[* TO '
+
facet
.
rangeStart
+
'-1SECOND]'
;
return
rangeFormatter
(
'*'
,
facet
.
rangeStart
+
'-1SECOND'
)
;
}
fromDate
=
new
Date
(
key
);
toDate
=
new
Date
(
key
);
toDate
.
setUTCFullYear
(
toDate
.
getUTCFullYear
()
+
1
);
return
'['
+
fromDate
.
toISOString
()
+
' TO '
+
toDate
.
toISOString
()
+
']'
;
return
rangeFormatter
(
fromDate
,
toDate
)
;
};
if
(
facet
.
other
)
{
facetOptions
[
'facet.range.other'
]
=
facet
.
other
;
}
break
;
case
'range'
:
case
'interval'
:
valueFormatter
=
function
(
item
)
{
var
key
=
parseInt
(
item
.
get
(
'key'
));
return
'['
+
key
+
' TO '
+
(
key
+
facet
.
rangeGap
)
+
']'
;
return
rangeFormatter
(
key
,
key
+
facet
.
rangeGap
);
};
break
;
case
'year-field'
:
type
=
'field'
;
quoteFormatter
=
function
(
value
)
{
return
new
Date
(
value
).
toISOString
();
};
valueFormatter
=
facet
.
queryValue
?
facet
.
queryValue
:
function
(
value
)
{
return
quoteFormatter
(
getItemKeyAccessor
(
value
));
};
_
.
each
(
facet
.
bins
,
function
(
includeValues
,
key
)
{
var
query
=
_
.
map
(
includeValues
,
function
(
includeValue
,
index
)
{
return
queryField
+
':\''
+
includeValue
+
'\''
;
}).
join
(
' OR '
);
result
[
'facet.query'
].
push
(
'{!key='
+
facetName
+
':'
+
key
+
' tag='
+
facetName
+
':'
+
key
+
'}('
+
query
+
')'
);
});
break
;
case
'field'
:
valueFormatter
=
facet
.
queryValue
?
facet
.
queryValue
:
getItemKeyAccessor
;
valueFormatter
=
facet
.
queryValue
?
facet
.
queryValue
:
function
(
value
)
{
return
quoteFormatter
(
getItemKeyAccessor
(
value
));
};
_
.
each
(
facet
.
bins
,
function
(
includeValues
,
key
)
{
var
query
=
_
.
map
(
includeValues
,
function
(
includeValue
,
index
)
{
return
queryField
+
':'
+
includeValue
;
...
...
@@ -174,8 +283,16 @@ define(function(require) {
facetOptions
[
'facet.range.end'
]
=
facet
.
rangeEnd
;
facetOptions
[
'facet.range.gap'
]
=
facet
.
rangeGap
;
break
;
case
'interval'
:
facetOptions
[
'facet.interval.set'
]
=
'[*,*]'
;
break
;
}
var
facetValues
=
[];
var
queryMax
=
facet
.
get
(
'queryMax'
);
var
queryMin
=
facet
.
get
(
'queryMin'
);
if
(
queryMax
!==
undefined
&&
queryMin
!==
undefined
)
{
facetValues
.
push
(
rangeFormatter
(
queryMin
,
queryMax
));
}
facet
.
get
(
'items'
).
each
(
function
(
item
,
index
)
{
if
(
!
item
.
get
(
'hidden'
)
&&
item
.
get
(
'checked'
))
{
facetValues
.
push
(
valueFormatter
(
item
));
...
...
@@ -184,22 +301,61 @@ define(function(require) {
facetOptions
.
key
=
facetName
;
if
(
facetValues
.
length
)
{
facetOptions
.
ex
=
facetName
;
result
.
fq
.
push
(
'{!tag='
+
facetName
+
'}'
+
query
Field
+
':('
+
facetValues
.
join
(
' OR '
)
+
')'
);
result
.
fq
.
push
(
'{!tag='
+
facetName
+
'}'
+
facet
Field
+
':('
+
facetValues
.
join
(
' OR '
)
+
')'
);
}
else
{
facetOptions
.
tag
=
facetName
;
}
if
(
!!
options
.
facetSuggestions
)
{
var
childFacet
=
options
.
childFacet
;
if
(
childFacet
===
facet
)
{
facetOptions
[
'facet.contains'
]
=
facet
.
get
(
'query'
);
facetOptions
[
'facet.contains.ignoreCase'
]
=
true
;
facetOptions
[
'facet.mincount'
]
=
1
;
}
}
var
facetOptionList
=
[];
_
.
each
(
facetOptions
,
function
(
value
,
key
)
{
if
(
false
&&
key
.
indexOf
(
'facet.'
)
===
0
)
{
result
[
'f.'
+
queryField
+
'.'
+
key
]
=
value
;
}
else
{
if
(
value
!==
undefined
)
{
facetOptionList
.
push
(
key
+
'='
+
value
);
}
});
result
[
'facet.'
+
type
].
push
(
'{!'
+
facetOptionList
.
join
(
' '
)
+
'}'
+
queryField
);
if
(
facet
.
facetStats
)
{
result
[
'stats.field'
].
push
(
'{!'
+
facetOptionList
.
join
(
' '
)
+
'}'
+
queryField
);
}
var
facetQuery
=
facet
.
get
(
'query'
);
if
(
facetQuery
!==
null
&&
facetQuery
!==
''
)
{
facetOptions
.
key
=
'suggestions:'
+
facetName
;
facetOptionList
=
[];
_
.
each
(
facetOptions
,
function
(
value
,
key
)
{
if
(
value
!==
undefined
)
{
facetOptionList
.
push
(
key
+
'='
+
value
);
}
});
result
[
'facet.'
+
type
].
push
(
'{!'
+
facetOptionList
.
join
(
' '
)
+
'}'
+
queryField
);
}
},
this
));
return
result
;
}
},
toJSON
:
function
toJSON
(
options
)
{
if
(
!!
options
.
facetSuggestions
)
{
return
_
.
extend
({
rows
:
0
,
facet
:
true
,
wt
:
'json'
,
q
:
'*:*'
,
},
this
.
getFacetFormData
(
options
));
}
},
parse
:
function
parse
(
data
,
options
)
{
if
(
!!
options
.
facetSuggestions
)
{
this
.
applyFacetResults
(
data
,
options
);
}
return
null
;
},
}));
var
Sort
=
Facets
.
Sort
=
{
standard
:
function
standardSort
(
a
,
b
)
{
...
...
@@ -241,31 +397,95 @@ define(function(require) {
};
}
var
Facet
=
Facets
.
Facet
=
Backbone
.
Model
.
extend
({
defaults
:
function
defaults
()
{
return
{
formName
:
null
,
query
:
null
,
suggestions
:
[],
};
},
initialize
:
function
(
data
,
options
)
{
this
.
set
(
'all'
,
true
);
this
.
on
(
'change:queryMax change:queryMin'
,
function
()
{
this
.
set
(
'hasQuery'
,
this
.
get
(
'queryMin'
)
!==
undefined
&&
this
.
get
(
'queryMax'
)
!==
undefined
);
},
this
);
(
function
(
self
)
{
var
skipCallback
;
self
.
on
(
'item-change'
,
function
()
{
if
(
skipCallback
===
'change:all'
)
{
return
;
self
.
on
(
'item-change change:queryMax change:queryMin'
,
function
()
{
switch
(
skipCallback
)
{
case
'change:checkedKeys'
:
case
'change:all'
:
return
;
}
var
checkedKeys
=
[];
var
checkedCount
this
.
get
(
'items'
).
each
(
function
(
facetItem
)
{
if
(
facetItem
.
get
(
'checked'
))
{
checkedKeys
.
push
(
facetItem
.
get
(
'key'
));
}
});
var
checkedCount
=
checkedKeys
.
length
;
if
(
this
.
get
(
'queryMin'
)
!==
undefined
&&
this
.
get
(
'queryMax'
)
!==
undefined
)
{
checkedCount
++
;
}
var
checkedCount
=
this
.
get
(
'items'
).
countBy
(
function
(
facetItem
)
{
return
facetItem
.
get
(
'checked'
);
}).
true
;
skipCallback
=
'item-change'
;
try
{
this
.
set
(
'all'
,
checkedCount
===
0
);
this
.
set
({
all
:
checkedCount
===
0
,
checkedKeys
:
checkedKeys
,
});
}
finally
{
skipCallback
=
null
;
}
},
self
);
self
.
on
(
'change:all'
,
function
()
{
if
(
skipCallback
===
'item-change'
)
{
return
;
switch
(
skipCallback
)
{
case
'change:checkedKeys'
:
case
'item-change'
:
return
;
}
skipCallback
=
'change:all'
;
try
{
this
.
get
(
'items'
).
invoke
(
'set'
,
'checked'
,
false
);
this
.
get
(
'items'
).
invoke
(
'set'
,
{
checked
:
false
});
this
.
set
({
checkedKeys
:
[],
queryMax
:
undefined
,
queryMin
:
undefined
,
});
}
finally
{
skipCallback
=
null
;
}
},
self
);
self
.
on
(
'change:checkedKeys'
,
function
()
{
switch
(
skipCallback
)
{
case
'item-change'
:
case
'change:all'
:
return
;
}
var
checkedKeys
=
{};
_
.
each
(
this
.
get
(
'checkedKeys'
),
function
(
value
,
i
)
{
checkedKeys
[
value
]
=
true
;
});
skipCallback
=
'change:checkedKeys'
;
var
checkedCount
=
0
;
if
(
this
.
get
(
'queryMin'
)
!==
undefined
&&
this
.
get
(
'queryMax'
)
!==
undefined
)
{
checkedCount
++
;
}
try
{
this
.
get
(
'items'
).
each
(
function
(
facetItem
)
{
var
newChecked
=
checkedKeys
[
facetItem
.
get
(
'key'
)];
var
currentChecked
=
facetItem
.
get
(
'checked'
);
if
(
newChecked
!==
currentChecked
)
{
facetItem
.
set
(
'checked'
,
newChecked
);
}
if
(
facetItem
.
get
(
'checked'
))
{
checkedCount
++
;
}
});
this
.
set
({
all
:
checkedCount
===
0
,
});
}
finally
{
skipCallback
=
null
;
}
...
...
@@ -284,6 +504,7 @@ define(function(require) {
switch
(
options
.
facetType
)
{
case
'year'
:
case
'range'
:
case
'interval'
:
comparator
=
wrapComparatorForAllBeforeAfter
(
comparator
,
[
'all'
,
'before'
].
indexOf
(
options
.
other
)
!==
-
1
,
[
'all'
,
'after'
].
indexOf
(
options
.
other
)
!==
-
1
);
break
;
}
...
...
@@ -317,7 +538,9 @@ define(function(require) {
})(
comparator
);
this
.
set
(
'items'
,
new
ItemCollection
([],
{
comparator
:
comparator
}));
this
.
facetType
=
options
.
facetType
;
this
.
facetStats
=
options
.
facetStats
;
this
.
other
=
options
.
other
;
this
.
facetField
=
options
.
facetField
;
this
.
queryField
=
options
.
queryField
;
this
.
queryValue
=
options
.
queryValue
;
this
.
bins
=
options
.
bins
;
...
...
@@ -333,6 +556,8 @@ define(function(require) {
this
.
rangeGap
=
options
.
gap
;
break
;
case
'field'
:
case
'year-field'
:
case
'interval'
:
break
;
default
:
var
e
=
new
Error
(
'Unsupported facet type: '
+
this
.
facetType
);
...
...
@@ -340,7 +565,40 @@ define(function(require) {
throw
e
;
}
return
Facet
.
__super__
.
initialize
.
apply
(
this
,
arguments
);
}
},
/*
toJSON: function toJSON(options) {
if (!!options.facetSuggestions) {
return {
rows: 0,
facet: true,
wt: 'json',
q: '*:*',
'facet.contains': this.get('query'),
'facet.contains.ignoreCase': true,
'facet.field': this.queryField,
'facet.mincount': 1,
};
}
},
parse: function parse(data, options) {
if (!!options.facetSuggestions) {
var suggestions = [];
var key;
_.each(data.facet_counts.facet_fields[this.queryField], function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
suggestions.push({key: key, value: value});
}
});
return {
suggestions: suggestions,
};
}
return null;
},
*/
});
var
ItemCollection
=
Backbone
.
Collection
.
extend
({
remove
:
function
()
{
...
...
@@ -419,7 +677,7 @@ define(function(require) {
startAt
=
1
;
}
if
(
endAt
-
startAt
<
9
)
{
/* global console:
false */
/* global console:false */
console
.
log
(
'foo'
);
}
...
...
@@ -464,7 +722,7 @@ define(function(require) {
next
=
_
.
bind
(
function
(
e
)
{
e
.
preventDefault
();
this
.
set
(
'currentPage'
,
this
.
get
(
'currentPage'
)
+
pageCount
);
return
tru
e
;
return
fals
e
;
},
this
);
}
else
{
next
=
stepFalse
;
...
...
@@ -473,7 +731,7 @@ define(function(require) {
previous
=
_
.
bind
(
function
(
e
)
{
e
.
preventDefault
();
this
.
set
(
'currentPage'
,
this
.
get
(
'currentPage'
)
-
pageCount
);
return
tru
e
;
return
fals
e
;
},
this
);
}
else
{
previous
=
stepFalse
;
...
...
@@ -505,6 +763,14 @@ define(function(require) {
value
:
null
,
items
:
new
Backbone
.
Collection
(),
},
initialize
:
function
(
data
,
options
)
{
if
(
this
.
get
(
'value'
)
===
null
)
{
var
firstItem
=
this
.
get
(
'items'
).
at
(
0
);
if
(
firstItem
)
{
this
.
set
(
'value'
,
firstItem
.
get
(
'value'
));
}
}
},
parse
:
function
(
data
)
{
var
result
=
_
.
clone
(
data
);
if
(
result
.
items
&&
!
(
result
.
items
instanceof
Backbone
.
Collection
))
{
...
...
@@ -513,94 +779,173 @@ define(function(require) {
return
result
;
},
});
var
QueryTextField
=
Backbone
.
Model
.
extend
({
defaults
:
{
formName
:
null
,
name
:
null
,
queries
:
[],
fields
:
null
,
multi
:
false
},
});
var
SolrSearch
=
Pagination
.
extend
({
url
:
function
url
()
{
return
this
.
constructor
.
selectUrl
;
},
defaults
:
function
defaults
()
{
var
constructor
=
this
.
constructor
;
var
formNameMap
=
{};
var
facets
=
new
SolrFacets
(
this
.
constructor
.
facets
,
{
search
:
this
});
_
.
each
(
facets
.
values
(),
function
(
facet
)
{
var
formName
=
facet
.
get
(
'formName'
);
if
(
formName
)
{
formNameMap
[
formName
]
=
facet
;
}
});
var
queryFields
=
new
Backbone
.
Model
();
_
.
each
(
constructor
.
queryTextFields
,
function
(
definition
,
queryName
)
{
var
qtf
=
new
QueryTextField
({
formName
:
definition
.
formName
,
name
:
queryName
,
queries
:
[],
fields
:
definition
.
fields
,
multi
:
!!
definition
.
multi
});
var
formName
=
qtf
.
get
(
'formName'
);
if
(
formName
)
{
formNameMap
[
formName
]
=
qtf
;
}
queryFields
.
set
(
queryName
,
qtf
);
},
this
);
return
_
.
extend
(
_
.
result
(
SolrSearch
.
__super__
,
'defaults'
),
{
initializing
:
true
,
initialized
:
false
,
query
:
''
,
formNameMap
:
formNameMap
,
results
:
new
Backbone
.
Collection
(),
ordering
:
new
Ordering
({
items
:
this
.
constructor
.
orderingItems
},
{
parse
:
true
}),
facets
:
new
SolrFacets
(
this
.
constructor
.
facets
),
ordering
:
new
Ordering
({
items
:
constructor
.
orderingItems
},
{
parse
:
true
}),
facets
:
facets
,
queryFields
:
queryFields
,
});
},
applyQueryParameters
:
function
()
{
var
skipOptions
=
{
skipSearch
:
true
};
var
parts
=
document
.
location
.
href
.
match
(
/.*
\?(
.*
)
/
);
var
facets
=
this
.
get
(
'facets'
);
facets
.
resetSearch
();
_
.
each
(
this
.
get
(
'queryFields'
).
values
(),
function
(
qtf
)
{
qtf
.
set
({
query
:
null
,
queries
:
[],
},
skipOptions
);
});
if
(
parts
)
{
var
formNameMap
=
this
.
get
(
'formNameMap'
);
var
keyValueParts
=
parts
[
1
].
split
(
'&'
);
for
(
var
i
=
0
;
i
<
keyValueParts
.
length
;
i
++
)
{
var
keyFieldValue
=
keyValueParts
[
i
].
match
(
/^
([^
.
]
+
)(?:\.([^
.
]
+
))?
=
(
.*
)
$/
);
if
(
keyFieldValue
)
{
var
key
=
keyFieldValue
[
1
];
var
field
=
keyFieldValue
[
2
];
var
value
=
keyFieldValue
[
3
];
value
=
value
.
replace
(
/
(\+
|%20
)
/g
,
' '
);
var
impl
=
formNameMap
[
key
];
if
(
impl
)
{
if
(
impl
instanceof
QueryTextField
)
{
impl
.
set
(
'query'
,
value
,
skipOptions
);
}
else
if
(
impl
.
facetType
===
'field'
&&
value
)
{
if
(
field
===
'min'
)
{
impl
.
set
(
'queryMin'
,
parseInt
(
value
),
skipOptions
);
}
else
if
(
field
===
'max'
)
{
impl
.
set
(
'queryMax'
,
parseInt
(
value
),
skipOptions
);
}
else
if
(
field
===
'items'
)
{
var
items
=
impl
.
get
(
'items'
);
var
item
=
items
.
get
(
value
);
if
(
!
item
)
{
item
=
new
SolrFacets
.
Facet
.
Item
({
key
:
value
,
value
:
0
});
items
.
add
(
item
);
item
.
on
(
'change:checked'
,
function
(
model
,
value
,
options
)
{
this
.
trigger
(
'item-change'
,
null
,
null
,
options
);
impl
.
trigger
(
'item-change'
,
null
,
null
,
options
);
},
facets
);
}
item
.
set
(
'checked'
,
true
,
skipOptions
);
}
}
}
}
}
}
},
initialize
:
function
(
data
,
options
)
{
console
.
log
(
'SolrSearch:initialize'
);
options
=
(
options
||
{});
this
.
set
(
'options'
,
new
Backbone
.
Model
({
faceting
:
!!
options
.
faceting
,
highlighting
:
!!
options
.
highlighting
,
showAll
:
!!
options
.
showAll
,
}));
this
.
_resetItems
=
{};
this
.
_doSearch
=
_
.
debounce
(
_
.
bind
(
function
()
{
console
.
log
(
'doSearch[b], about to call fetch'
);
this
.
fetch
({
this
.
_doSearchImmediately
=
_
.
bind
(
function
(
options
)
{
this
.
fetch
(
_
.
extend
({},
options
,
{
data
:
this
.
toJSON
(),
merge
:
false
,
traditional
:
true
,
});
},
this
),
250
);
}));
},
this
);
this
.
_doSearch
=
_
.
debounce
(
this
.
_doSearchImmediately
,
250
);
this
.
_doSearch
=
(
function
(
doSearch
)
{
return
function
()
{
console
.
log
(
'doSearch[a]'
,
this
.
_skipSearch
)
;
if
(
!
this
.
_
skipSearch
)
{
doSearch
();
return
function
(
options
)
{
var
skipSearch
=
this
.
_skipSearch
||
(
options
||
{}).
skipSearch
;
if
(
!
skipSearch
)
{
doSearch
(
options
);
}
};
})(
this
.
_doSearch
);
this
.
get
(
'options'
).
on
(
'change:faceting change:highlighting'
,
this
.
_doSearch
);
this
.
on
(
'change:query'
,
function
()
{
_
.
each
(
this
.
get
(
'queryFields'
).
values
(),
function
(
subQueryModel
)
{
subQueryModel
.
on
(
'change:query change:queries'
,
function
(
model
,
value
,
options
)
{
this
.
trigger
(
'change:queryFields'
,
null
,
null
,
options
);
},
this
);
},
this
);
this
.
on
(
'change:queryFields'
,
function
(
model
,
value
,
options
)
{
this
.
trigger
(
'change:query'
,
null
,
null
,
options
);
},
this
);
this
.
get
(
'options'
).
on
(
'change:faceting change:highlighting'
,
function
(
model
,
value
,
options
)
{
this
.
_doSearch
(
options
);
},
this
);
this
.
on
(
'change:query'
,
function
(
model
,
value
,
options
)
{
// this is silent, which causes the change events to not fire;
// this is ok, because the next .on will also see the change
// event on query, and resend the search
this
.
_
resetItems
.
currentPage
=
true
;
this
.
_resetItems
.
facets
=
true
;
this
.
_resetItems
.
ordering
=
true
;
this
.
_doSearch
();
this
.
_
doSearch
(
_
.
extend
({},
options
,
{
resetCurrentPage
:
true
,
resetFacets
:
true
,
resetOrdering
:
true
}))
;
},
this
)
;
this
.
on
(
'change:pageSize'
,
function
(
model
,
value
,
options
)
{
this
.
_doSearch
(
_
.
extend
({},
options
,
{
resetCurrentPage
:
true
})
);
},
this
);
this
.
on
(
'change:pageSize'
,
function
()
{
this
.
_resetItems
.
currentPage
=
true
;
this
.
_doSearch
();
this
.
on
(
'change:currentPage'
,
function
(
model
,
value
,
options
)
{
this
.
_doSearch
(
options
);
},
this
);
this
.
on
(
'change:currentPage'
,
this
.
_doSearch
);
this
.
get
(
'facets'
).
on
(
'item-change'
,
function
()
{
this
.
_resetItems
.
currentPage
=
true
;
this
.
_doSearch
();
this
.
get
(
'facets'
).
on
(
'item-change'
,
function
(
model
,
value
,
options
)
{
this
.
_doSearch
(
_
.
extend
({},
options
,
{
resetCurrentPage
:
true
}));
},
this
);
this
.
get
(
'ordering'
).
on
(
'change:value'
,
function
()
{
this
.
_resetItems
.
currentPage
=
true
;
this
.
_doSearch
();
this
.
get
(
'ordering'
).
on
(
'change:value'
,
function
(
model
,
value
,
options
)
{
this
.
_doSearch
(
_
.
extend
({},
options
,
{
resetCurrentPage
:
true
}));
},
this
);
return
SolrSearch
.
__super__
.
initialize
.
apply
(
this
,
arguments
);
},
parse
:
function
parse
(
data
,
options
)
{
// console.debug('SEARCH', data);
this
.
_skipSearch
=
true
;
try
{
var
resetItems
=
this
.
_resetItems
;
if
(
resetItems
.
currentPage
)
{
this
.
set
(
'currentPage'
,
1
);
}
if
(
resetItems
.
facets
)
{
this
.
get
(
'facets'
).
resetSearch
();
}
if
(
resetItems
.
ordering
)
{
this
.
get
(
'ordering'
).
set
(
'value'
,
this
.
get
(
'ordering'
).
get
(
'items'
).
at
(
0
).
get
(
'value'
));
}
}
finally
{
delete
this
.
_skipSearch
;
if
(
options
.
facetSuggestions
)
{
return
null
;
}
var
skipOptions
=
_
.
extend
({},
options
,
{
skipSearch
:
true
});
if
(
options
.
resetCurrentPage
)
{
this
.
set
(
'currentPage'
,
1
,
skipOptions
);
}
if
(
options
.
resetFacets
)
{
this
.
get
(
'facets'
).
resetSearch
(
skipOptions
);
}
if
(
options
.
resetOrdering
)
{
this
.
get
(
'ordering'
).
set
(
'value'
,
this
.
get
(
'ordering'
).
get
(
'items'
).
at
(
0
).
get
(
'value'
),
skipOptions
);
}
this
.
_resetItems
=
{};
var
facets
=
this
.
get
(
'facets'
);
if
(
this
.
get
(
'options'
).
get
(
'faceting'
))
{
facets
.
applyFacetResults
(
data
);
console
.
log
(
'facets'
,
facets
);
}
else
{
facets
.
resetSearch
();
facets
.
resetSearch
(
options
);
}
var
list
=
[];
var
highlighting
=
this
.
get
(
'options'
).
get
(
'highlighting'
)
?
data
.
highlighting
:
{};
...
...
@@ -631,7 +976,7 @@ define(function(require) {
_
.
each
(
fieldsToParse
,
function
(
value
,
key
)
{
var
parsed
;
if
(
_
.
isFunction
(
value
))
{
parsed
=
value
.
call
(
this
,
doc
,
index
,
itemHighlighting
);
parsed
=
value
.
call
(
this
,
doc
,
index
,
itemHighlighting
,
key
);
}
else
if
(
_
.
isArray
(
value
))
{
parsed
=
[];
_
.
each
(
value
,
function
(
item
,
i
)
{
...
...
@@ -650,41 +995,83 @@ define(function(require) {
},
this
);
return
result
;
},
toJSON
:
function
()
{
var
currentPage
=
this
.
get
(
'currentPage'
);
var
pageSize
=
this
.
get
(
'pageSize'
);
var
sort
=
this
.
get
(
'ordering'
).
get
(
'value'
);
var
query
=
this
.
get
(
'query'
);
query
=
query
.
replace
(
/:+/g
,
' '
);
if
(
this
.
_resetItems
.
currentPage
)
{
currentPage
=
1
;
}
if
(
this
.
_resetItems
.
ordering
)
{
sort
=
this
.
get
(
'ordering'
).
get
(
'items'
).
at
(
0
).
get
(
'value'
);
toJSON
:
function
(
options
)
{
if
(
!
options
)
{
options
=
{};
}
var
facets
=
this
.
get
(
'facets'
);
var
constructor
=
this
.
constructor
;
var
queryFilter
=
constructor
.
queryFilter
;
var
result
=
{
defType
:
'dismax'
,
qf
:
queryFilter
?
queryFilter
:
'text'
,
sort
:
sort
,
//'f.sku.hl.fragmenter': 'regex',
//'f.sku.hl.mergeContiguous': 'true',
//'f.sku.hl.regex.pattern': '[a-zA-Z]+-[0-9]{4}-[0-9]{1}-[a-zA-Z]{1}-[0-9]+',
//'f.sku.hl.regex.pattern': '...-....-..-.-....',
fl
:
'*,score'
,
rows
:
pageSize
,
start
:
(
currentPage
-
1
)
*
pageSize
,
wt
:
'json'
defType
:
'edismax'
,
qf
:
constructor
.
queryField
,
wt
:
'json'
,
};
result
.
fl
=
mergeStaticProps
(
this
.
constructor
,
SolrSearch
,
[],
'returnFields'
);
var
ordering
=
this
.
get
(
'ordering'
);
var
sort
=
ordering
.
get
(
'value'
);
if
(
options
.
resetOrdering
)
{
sort
=
ordering
.
get
(
'items'
).
at
(
0
).
get
(
'value'
);
}
var
currentPage
=
this
.
get
(
'currentPage'
);
var
pageSize
=
this
.
get
(
'pageSize'
);
if
(
options
.
resetCurrentPage
)
{
currentPage
=
1
;
}
result
.
sort
=
sort
;
result
.
rows
=
pageSize
;
result
.
start
=
(
currentPage
-
1
)
*
pageSize
;
result
.
fl
=
mergeStaticSets
(
constructor
,
SolrSearch
,
[],
'returnFields'
);
if
(
this
.
get
(
'options'
).
get
(
'highlighting'
))
{
result
.
hl
=
true
;
result
[
'hl.fl'
]
=
[
'content'
,
'title'
].
concat
(
constructor
.
extraHighlightFields
);
}
if
(
this
.
get
(
'options'
).
get
(
'faceting'
))
{
var
facetFormData
=
facets
.
getFacetFormData
();
var
fq
=
facetFormData
.
fq
;
delete
(
facetFormData
.
fq
);
result
=
_
.
extend
(
result
,
facetFormData
);
if
(
fq
.
length
)
{
if
(
result
.
fq
)
{
result
.
fq
=
result
.
fq
.
concat
(
fq
);
}
else
{
result
.
fq
=
fq
;
}
}
}
var
queryParts
=
[];
var
queryFields
=
this
.
get
(
'queryFields'
);
_
.
each
(
queryFields
.
keys
(),
function
(
queryName
)
{
var
subQueryModel
=
queryFields
.
get
(
queryName
);
var
fields
=
subQueryModel
.
get
(
'fields'
);
var
queries
=
subQueryModel
.
get
(
'queries'
);
var
query
=
subQueryModel
.
get
(
'query'
);
if
(
query
)
{
queries
=
queries
.
concat
([
query
]);
}
var
subQuery
=
[];
for
(
var
i
=
0
;
i
<
fields
.
length
;
i
++
)
{
var
fieldName
=
fields
[
i
];
for
(
var
j
=
0
;
j
<
queries
.
length
;
j
++
)
{
// FIXME: quote the query
subQuery
.
push
(
fieldName
+
':\''
+
queries
[
j
]
+
'\''
);
}
}
if
(
subQuery
.
length
)
{
queryParts
.
push
(
subQuery
.
join
(
' OR '
));
}
},
this
);
var
query
=
queryParts
.
join
(
' AND '
);
if
(
!
query
)
{
query
=
'*:*'
;
if
(
!
this
.
get
(
'options'
).
get
(
'showAll'
)
&&
!
result
.
fq
)
{
result
.
rows
=
0
;
}
}
var
dateBoostField
=
constructor
.
dateBoostField
;
var
popularityBoostField
=
constructor
.
popularityBoostField
;
if
(
dateBoostField
||
popularityBoostField
)
{
if
(
result
.
rows
!==
0
&&
(
dateBoostField
||
popularityBoostField
)
)
{
var
boostSum
=
[];
if
(
dateBoostField
)
{
result
.
dateBoost
=
'recip(ms(NOW,'
+
dateBoostField
+
'),3.16e-11,1,1)'
;
...
...
@@ -699,19 +1086,6 @@ define(function(require) {
}
else
{
result
.
q
=
query
;
}
if
(
this
.
get
(
'options'
).
get
(
'faceting'
))
{
var
facetFormData
=
this
.
get
(
'facets'
).
getFacetFormData
();
var
fq
=
facetFormData
.
fq
;
delete
(
facetFormData
.
fq
);
result
=
_
.
extend
(
result
,
facetFormData
);
if
(
fq
.
length
)
{
if
(
result
.
fq
)
{
result
.
fq
=
result
.
fq
.
concat
(
fq
);
}
else
{
result
.
fq
=
fq
;
}
}
}
return
result
;
},
},
{
...
...
Please
register
or
sign in
to post a comment