SOURCE

// const data = [
//   { id: 1, scontent: "铜板烧,餐饮,1F,一家专做烧烤的店" },
//   { id: 2, scontent: "711,餐饮,1F,一家便利店" },
//   { id: 3, scontent: "泉森药妆,美妆,2F,全球最全的化妆品" },
// ];

// const invertedIndex = {};

// data.forEach(item => {
//   const tokens = item.scontent.split(',');
//   tokens.forEach(token => {
//     if (!invertedIndex[token]) {
//       invertedIndex[token] = [];
//     }
//     invertedIndex[token].push(item.id);
//   });
// });

// function tokenize(query) {
//   // 简单的分词函数,将“1楼”转换为“1F”,并按空格分词
//   return query.replace('1楼', '1F').split(' ').filter(token => token !== '有什么');
// }

// function search(query) {
//   const tokens = tokenize(query);
//   const results = tokens.map(token => invertedIndex[token] || []).reduce((acc, curr) => {
//     return acc.filter(id => curr.includes(id));
//   }, data.map(item => item.id));
//   return results;
// }

// console.log(search("1楼有什么餐饮"));  // [1, 2]


// const data = [
//     {
//         id: 1,
//         scontent: "铜板烧,餐饮,吃,1F,一家专做烧烤的店"
//     },
//     {
//         id: 2,
//         scontent: "711,餐饮,吃,1F,一家便利店"
//     },
//     {
//         id: 3,
//         scontent: "泉森药妆,美妆,买,2F,全球最全的化妆品"
//     }
// ];

const data = [
    {
        "id": 1,
        "name": "seven eleven",
        "name_tc": "711",
        "name_zh": "711",
        "floor": "G",
        "type": "購物消閒",
        "bz": "711,seven eleven,1F,1樓,便利店,日用品,食物,飲品,1楼,便利店,日用品,食物,饮品",
        "createdAt": "2023-08-22T03:26:31.413Z",
        "updatedAt": "2023-08-23T16:44:24.814Z",
        "tag": [
            "便利店",
            "日用品",
            "食物",
            "飲品"
        ],
        "modelId": "7072050339842756608",
        "img": {
            "id": 3,
            "name": "20181116134851.jpeg",
            "alternativeText": null,
            "caption": null,
            "width": 400,
            "height": 400,
            "formats": {
                "thumbnail": {
                    "ext": ".jpeg",
                    "url": "/uploads/thumbnail_20181116134851_fca9ffb8d4.jpeg",
                    "hash": "thumbnail_20181116134851_fca9ffb8d4",
                    "mime": "image/jpeg",
                    "name": "thumbnail_20181116134851.jpeg",
                    "path": null,
                    "size": 3.91,
                    "width": 156,
                    "height": 156
                }
            },
            "hash": "20181116134851_fca9ffb8d4",
            "ext": ".jpeg",
            "mime": "image/jpeg",
            "size": 11.52,
            "url": "/uploads/20181116134851_fca9ffb8d4.jpeg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "createdAt": "2023-08-06T09:26:35.528Z",
            "updatedAt": "2023-08-06T09:26:35.528Z"
        }
    },
    {
        "id": 2,
        "name": "GENKI SUSHI",
        "name_tc": "元気寿司",
        "name_zh": "元氣壽司",
        "floor": "2",
        "type": "餐飲美饌",
        "bz": "GENKI SUSHI,元氣壽司,2F,2樓,二樓,餐飲,日本料理,刺身,壽司,食物,元气寿司,2F,2楼,二楼,餐饮,日本料理,刺身,寿司,食物",
        "createdAt": "2023-08-22T03:32:01.782Z",
        "updatedAt": "2023-08-23T16:44:03.003Z",
        "tag": [
            "日本料理",
            "刺身",
            "壽司",
            "食物"
        ],
        "modelId": "7095705380285714434",
        "img": {
            "id": 49,
            "name": "20230130094500_989be026b2a9e74d155c89b011181652.jpeg",
            "alternativeText": null,
            "caption": null,
            "width": 400,
            "height": 400,
            "formats": {
                "thumbnail": {
                    "ext": ".jpeg",
                    "url": "/uploads/thumbnail_20230130094500_989be026b2a9e74d155c89b011181652_f002150958.jpeg",
                    "hash": "thumbnail_20230130094500_989be026b2a9e74d155c89b011181652_f002150958",
                    "mime": "image/jpeg",
                    "name": "thumbnail_20230130094500_989be026b2a9e74d155c89b011181652.jpeg",
                    "path": null,
                    "size": 5.29,
                    "width": 156,
                    "height": 156
                }
            },
            "hash": "20230130094500_989be026b2a9e74d155c89b011181652_f002150958",
            "ext": ".jpeg",
            "mime": "image/jpeg",
            "size": 17.34,
            "url": "/uploads/20230130094500_989be026b2a9e74d155c89b011181652_f002150958.jpeg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "createdAt": "2023-08-22T03:31:56.971Z",
            "updatedAt": "2023-08-22T03:31:56.971Z"
        }
    },
    {
        "id": 3,
        "name": "Café de Coral",
        "name_tc": "大家樂",
        "name_zh": "大家樂",
        "floor": "G",
        "type": "餐飲美饌",
        "bz": "Café de Coral,大家樂,1F,1樓,一樓,早餐,中餐,晚餐,食物,餐飲,大家乐,1F,1楼,一楼,早餐,中餐,晚餐,食物,餐饮,",
        "createdAt": "2023-08-22T03:57:59.757Z",
        "updatedAt": "2023-08-23T16:43:27.634Z",
        "tag": [
            "食物",
            "早餐",
            "中餐",
            "晚餐"
        ],
        "modelId": "7072051448590241792",
        "img": {
            "id": 50,
            "name": "20210315145237_0edb170e68b64a5b6d4155ca8897a34a (1).jpg",
            "alternativeText": null,
            "caption": null,
            "width": 400,
            "height": 400,
            "formats": {
                "thumbnail": {
                    "ext": ".jpg",
                    "url": "/uploads/thumbnail_20210315145237_0edb170e68b64a5b6d4155ca8897a34a_1_6b68bfd449.jpg",
                    "hash": "thumbnail_20210315145237_0edb170e68b64a5b6d4155ca8897a34a_1_6b68bfd449",
                    "mime": "image/jpeg",
                    "name": "thumbnail_20210315145237_0edb170e68b64a5b6d4155ca8897a34a (1).jpg",
                    "path": null,
                    "size": 4.1,
                    "width": 156,
                    "height": 156
                }
            },
            "hash": "20210315145237_0edb170e68b64a5b6d4155ca8897a34a_1_6b68bfd449",
            "ext": ".jpg",
            "mime": "image/jpeg",
            "size": 14.18,
            "url": "/uploads/20210315145237_0edb170e68b64a5b6d4155ca8897a34a_1_6b68bfd449.jpg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "createdAt": "2023-08-22T03:56:53.765Z",
            "updatedAt": "2023-08-22T03:56:53.765Z"
        }
    },
    {
        "id": 4,
        "name": "manner dai pai dong",
        "name_tc": "微辣大排檔",
        "name_zh": "微辣大排档",
        "floor": "2",
        "type": "餐飲美饌",
        "bz": "manner dai pai dong,微辣大排檔,2F,2樓,二樓,餐飲,早餐,中餐,晚餐,食物,燒烤,微辣大排档,2F,2楼,二楼,餐饮,早餐,中餐,晚餐,食物,烧烤,",
        "createdAt": "2023-08-23T13:02:33.456Z",
        "updatedAt": "2023-08-23T16:43:01.894Z",
        "tag": [
            "燒烤",
            "早餐",
            "中餐",
            "晚餐",
            "飲品"
        ],
        "modelId": "7095705380285714434",
        "img": {
            "id": 28,
            "name": "website-LOGO.jpg",
            "alternativeText": null,
            "caption": null,
            "width": 384,
            "height": 384,
            "formats": {
                "thumbnail": {
                    "ext": ".jpg",
                    "url": "/uploads/thumbnail_website_LOGO_4054992ec4.jpg",
                    "hash": "thumbnail_website_LOGO_4054992ec4",
                    "mime": "image/jpeg",
                    "name": "thumbnail_website-LOGO.jpg",
                    "path": null,
                    "size": 3.44,
                    "width": 156,
                    "height": 156
                }
            },
            "hash": "website_LOGO_4054992ec4",
            "ext": ".jpg",
            "mime": "image/jpeg",
            "size": 12.53,
            "url": "/uploads/website_LOGO_4054992ec4.jpg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "createdAt": "2023-08-13T19:05:28.091Z",
            "updatedAt": "2023-08-13T19:05:28.091Z"
        }
    }
];
function search(query) {
    let keywords = query.split(" ");
    let newKeywords = [];
    const results = [];
    if (keywords.length == 1) {
        keywords = splitInput(keywords[0])
        let uniqueArray = [];
        console.log("keywords2:", keywords);
        for (let item of data) {
            let match = false; // 默认设置为false,表示当前项不匹配任何关键词

            for (let keyword of keywords) {
                if (item.bz.includes(keyword)) {
                    match = true; // 只要当前项的 "bz" 包含任何一个关键词,就设置为true

                    uniqueArray.push(keyword);

                    break; // 如果匹配了一个关键词,就可以退出内部循环了
                }
            }


        }

        newKeywords = removeDuplicates(uniqueArray)
        console.log("newKeywords:", newKeywords);

    }
    // 关联匹配关键词
    for (let item of data) {
        let match = true;
        for (let keyword of keywords) {
            if (!item.bz.includes(keyword)) {
                match = false;
                break;
            }
        }
        if (match) {
            results.push(item.id);
        }
    }

    console.log(results);
    return results;
}

// 分割关键词的方法
function splitInput(input) {
    const pairs = [];

    for (let i = 0; i < input.length - 1; i++) {
        const pair = input.slice(i, i + 2);
        pairs.push(pair);
    }

    const uniquePairs = Array.from(new Set(pairs));
    return uniquePairs;
}
// 去重的方法
function removeDuplicates(inputArray) {
    const uniqueArray = [];

    for (const item of inputArray) {
        if (!uniqueArray.includes(item)) {
            uniqueArray.push(item);
        }
    }

    return uniqueArray;
}

// const input = "微辣大大大初始";
// const result = searchItems(input,data);
// console.log(result);
// const input = "2樓 餐飲";
const input = "1楼有无寿司食";
const result = search(input);
console.log(result); // 输出 [1, 2]
console 命令行工具 X clear

                    
>
console