|
@@ -142,10 +142,68 @@ async function getDataByCode(code, data) {
|
|
|
|
|
|
|
|
|
async function getDataByKeyWord(keyword, data) {
|
|
|
+ //先按全字匹配
|
|
|
+ let fullResult =await getDataByFullKeyWord(keyword, data);
|
|
|
+ if(fullResult.totalSize > 0) return fullResult;
|
|
|
+
|
|
|
+ //如果没有才按模糊匹配
|
|
|
+ return await getDataByFuzzyMatch(keyword, data);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//按全关键字匹配
|
|
|
+async function getDataByFullKeyWord(keyword, data){
|
|
|
+ data.condition.name = new RegExp(keyword);
|
|
|
+ let items = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
|
|
|
+ delete data.condition.name;
|
|
|
+ return{totalSize:items.length,items}
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function handelThreeWord(word){
|
|
|
+ let arr = [];
|
|
|
+ getArr(word[0]+word[1],arr);
|
|
|
+ getArr(word[1]+word[2],arr);
|
|
|
+ if(arr.length > 0) return arr;
|
|
|
+ return[word];
|
|
|
+
|
|
|
+ function getArr(tem,list){
|
|
|
+ let nameArray = segment.doSegment(tem, {
|
|
|
+ simple: true, //不返回词性
|
|
|
+ stripPunctuation: true //去除标点符号
|
|
|
+ });
|
|
|
+ //说明是一个词
|
|
|
+ if(nameArray.length == 1) list.push(tem)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//自定义特殊处理
|
|
|
+function cusSegment(nameArray,keyword){
|
|
|
+ let temArr = [];
|
|
|
+ for (let a of nameArray) {
|
|
|
+ //混凝土 和 砼 统一认成砼
|
|
|
+ if (a == "混凝土") a = '砼';
|
|
|
+ if(a == '砼'||a == '砖' ){
|
|
|
+ temArr.push(a);
|
|
|
+ }else if(a.length > 1){
|
|
|
+ //如果是三个字的词,优先识别成两个字
|
|
|
+ if(a.length == 3){
|
|
|
+ let sArr = handelThreeWord(a);
|
|
|
+ temArr.push(...sArr);
|
|
|
+ }else{
|
|
|
+ temArr.push(a);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (keyword.length == 1 && temArr.length == 0) temArr.push(keyword);
|
|
|
+ return temArr;
|
|
|
+}
|
|
|
+
|
|
|
+//模糊匹配
|
|
|
+async function getDataByFuzzyMatch(keyword, data){
|
|
|
let items = [];
|
|
|
let nameArray = [];
|
|
|
- //混凝土 和 砼 认成一个
|
|
|
- // keyword = keyword.replace(/混凝土/g, "砼");
|
|
|
if (keyword.length < 3) {
|
|
|
nameArray.push(keyword)
|
|
|
} else {
|
|
@@ -154,13 +212,10 @@ async function getDataByKeyWord(keyword, data) {
|
|
|
stripPunctuation: true //去除标点符号
|
|
|
});
|
|
|
}
|
|
|
- let temArr = [];
|
|
|
- for (let a of nameArray) {
|
|
|
- if (a == "混凝土") a = '砼';
|
|
|
- if (a == '砼' || a.length > 1) temArr.push(a);
|
|
|
- }
|
|
|
- if (keyword.length == 1 && temArr.length == 0) temArr.push(keyword);
|
|
|
- nameArray = temArr;
|
|
|
+
|
|
|
+ //自定义处理
|
|
|
+ nameArray = cusSegment(nameArray,keyword);
|
|
|
+
|
|
|
console.log(nameArray);
|
|
|
|
|
|
let allInfoPrice = await infoItemsModel.find(data.condition).lean().sort({"_id":1});
|
|
@@ -172,6 +227,7 @@ async function getDataByKeyWord(keyword, data) {
|
|
|
//specs
|
|
|
let mstring = info.name + info.spec;
|
|
|
mstring = mstring.replace(/混凝土/g, "砼");
|
|
|
+ info.mstring = mstring;
|
|
|
let matchCount = 0;
|
|
|
for (let na of nameArray) {
|
|
|
if (mstring.indexOf(na) != -1) {
|
|
@@ -183,9 +239,16 @@ async function getDataByKeyWord(keyword, data) {
|
|
|
if (matchCount > maxNum) maxNum = matchCount;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
if (maxNum > 0) items = matchMap[maxNum];
|
|
|
totalSize = items.length
|
|
|
+
|
|
|
+ //按匹配位置排序 如[ '橡胶', '胶圈', '给水' ] 先显示橡胶
|
|
|
+ items = _.sortBy(items,(item)=>{
|
|
|
+ let ms = item.mstring;
|
|
|
+ for(let i = 0;i < nameArray.length;i ++){
|
|
|
+ if (ms.indexOf(nameArray[i]) != -1) return i;
|
|
|
+ }
|
|
|
+ })
|
|
|
return {totalSize,items}
|
|
|
}
|
|
|
|