Files
ofbiz-plugins/ecommerce/groovyScripts/content/Search.groovy
T
Jacques Le Roux 6fd8a8fac2 Fixed: Ecommerce component showing error screen on searchContent from
showcontenttree page
(OFBIZ-9309)

Steps to reproduce:
1.Go to Ecommerce component https://localhost:8443/ecommerce/control/main
2.Click on any of the Content from "Browse Content" section of left panel. 
  It will be directed to https://localhost:8443/ecommerce/control/showcontenttree?contentId=STORE_POLICIES&nodeTrailCsv=STORE_POLICIES
3. Click on Search, it directs to https://localhost:8443/ecommerce/control/searchContent?siteId=STORE_POLICIES
4. Screen renders with ScreenRenderingException

Problem:
1. "searchContent" request directs to "searchContent" screen in 
  ContentScreens.xml.
2. On searchContent screen, "/ecommerce/groovyScripts/content/Search.groovy" 
  script prepares and hits a search query to Lucene.
3. "BooleanQuery" class is used to match documents with other boolean query 
  combinations.
4. BooleanQuery object is initialised with Its default constructor.
5. With Lucene 5.4.0, this constructor is deprecated.
OFBiz migrated to Lucene 6.3.1: OFBIZ-8316

Solution:
From Lucene 5.4.0 default constructor is deprecated and a new inner class 
  Builder is created for queries. Refer OFBIZ-9301 for detailed explanation.

Thanks: Aditya

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-plugins/trunk@1790810 13f79535-47bb-0310-9956-ffa450edef68
2017-04-10 12:05:36 +00:00

107 lines
4.1 KiB
Groovy

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.lucene.analysis.Analyzer
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.document.Document
import org.apache.lucene.index.Term
import org.apache.lucene.queryparser.classic.QueryParser
import org.apache.lucene.store.FSDirectory
import org.apache.ofbiz.base.util.Debug
import org.apache.ofbiz.base.util.UtilHttp
import org.apache.ofbiz.content.search.SearchWorker
import org.apache.ofbiz.product.feature.ParametricSearch
import org.apache.lucene.search.*
import org.apache.lucene.index.DirectoryReader
import org.apache.lucene.store.Directory
import org.apache.ofbiz.base.util.UtilProperties
paramMap = UtilHttp.getParameterMap(request)
queryLine = paramMap.queryLine.toString()
//Debug.logInfo("in search, queryLine:" + queryLine, "")
siteId = paramMap.siteId ?: "WebStoreCONTENT"
//Debug.logInfo("in search, siteId:" + siteId, "")
featureIdByType = ParametricSearch.makeFeatureIdByTypeMap(paramMap)
//Debug.logInfo("in search, featureIdByType:" + featureIdByType, "")
combQuery = new BooleanQuery.Builder()
IndexSearcher searcher = null
Analyzer analyzer = null
try {
Directory directory = FSDirectory.open(new File(SearchWorker.getIndexPath("content")).toPath())
DirectoryReader reader = DirectoryReader.open(directory)
searcher = new IndexSearcher(reader)
analyzer = new StandardAnalyzer()
} catch (java.io.FileNotFoundException e) {
context.errorMessageList.add(UtilProperties.getMessage("ContentErrorUiLabels", "ContentSearchNotIndexed", locale))
return
}
termQuery = new TermQuery(new Term("site", siteId.toString()))
combQuery.add(termQuery, BooleanClause.Occur.MUST)
//Debug.logInfo("in search, termQuery:" + termQuery.toString(), "")
//Debug.logInfo("in search, combQuery(1):" + combQuery, "")
if (queryLine && analyzer) {
Query query = null
QueryParser parser = new QueryParser("content", analyzer)
query = parser.parse(queryLine)
combQuery.add(query, BooleanClause.Occur.MUST)
}
if (featureIdByType) {
featureQuery = new BooleanQuery.Builder()
featuresRequired = BooleanClause.Occur.MUST
if ("any".equals(paramMap.anyOrAll)) {
featuresRequired = BooleanClause.Occur.SHOULD
}
if (featureIdByType) {
featureIdByType.each { key, value ->
termQuery = new TermQuery(new Term("feature", value))
featureQuery.add(termQuery, featuresRequired)
//Debug.logInfo("in search searchFeature3, termQuery:" + termQuery.toString(), "")
}
}
combQuery.add(featureQuery.build(), featuresRequired)
}
if (searcher) {
Debug.logInfo("in search searchFeature3, combQuery:" + combQuery.toString(), "")
TopScoreDocCollector collector = TopScoreDocCollector.create(100) //defaulting to 100 results
searcher.search(combQuery.build(), collector)
ScoreDoc[] hits = collector.topDocs().scoreDocs
Debug.logInfo("in search, hits:" + collector.getTotalHits(), "")
contentList = [] as ArrayList
hitSet = [:] as HashSet
for (int start = 0; start < collector.getTotalHits(); start++) {
Document doc = searcher.doc(hits[start].doc)
contentId = doc.get("contentId")
content = from("Content").where("contentId", contentId).cache(true).queryOne()
if (!hitSet.contains(contentId)) {
contentList.add(content)
hitSet.add(contentId)
}
}
context.queryResults = contentList
}