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

Highlighting returns an error in some cases #480

Open
rgranotier opened this issue Mar 26, 2025 · 2 comments
Open

Highlighting returns an error in some cases #480

rgranotier opened this issue Mar 26, 2025 · 2 comments

Comments

@rgranotier
Copy link

As I was working with a larger document, I encountered two things:

  • The larger document in the example below creates the error Uncaught TypeError: x is undefined. If I remove the two last occurences of "Spain", the error does not happen anymore. This does not seem to be related to the amount of words nor the amount of instances of "Spain", but to the position of words after some threshold : "europe" or "capital" produce the same error, "middle" does not.

  • When highlighting works, the "highlighted" version of the output does not contain all the original non alpha-numeric symbols, is this intended?

Version: 0.8.138
Browser: Firefox

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="module">
    import {Charset, Document, IndexedDB} from "../node_modules/flexsearch/dist/flexsearch.bundle.module.debug.js";

    const index = new Document({
        document: {
            index: [
                {
                    field: "title",
                    tokenize: "forward",
                    encoder: Charset.LatinDefault
                },
                {
                    field: "text",
                    tokenize: "forward",
                    encoder: Charset.LatinDefault
                }
            ],
            store: true,
        },
        commit: false,
    });

    let all_docs = [
        {
            id: "469",
            title: "Polygon",
            text: "A polygon is a two dimensional figure"
        },
        {
            id: "888",
            title: "Spain",
            text: "Spain is a country."
        },
        {
            id: "473",
            text: "Madrid (pronounced: “mah-drid or /məˈdrɪd/) is the capital and largest city of Spain. Madrid is in the middle of Spain, in the Community of Madrid. The Community is a large area that includes the city as well as small towns and villages outside the city. 7 million people live in the Community. More than 3 million live in the city itself. It is the largest city of Spain and, at 655 m (2,100 ft) above sea level, the second highest capital in Europe (after the Andorran capital Andorra la Vella). It is the second largest city in the European Union. As it is the capital city, Madrid is where the monarch lives and also where the government meets. Madrid is the financial centre of Spain. Many large businesses have their main offices there. It has four important footballs teams, Real Madrid, Atlético Madrid, Getafe, and Rayo Vallecano. People who live in Madrid are called madrileños. Madrid was ruled by the Romans from the 2nd century. After AD 711 it was occupied by the Moors. In 1083 Spain was ruled again by Spaniards. Catholic kings ruled the country. By the mid-16th century it had become the capital of a very large empire. Spain was ruled by monarchs from the House of Habsburg, then the House of Bourbon. After the Spanish Civil War it was ruled by a dictator until the mid-1970s when it became a democracy. Although it is a modern city, a lot of its history can be seen and felt as one walks along the streets and in the large squares of the city. There are beautiful parks, famous buildings, art galleries and concert halls. == History == During the history of Spain many different people have lived there. Madrid's name comes from the Arabic word magerit, meaning “place of many streams\\\". The Phoenicians came in 1100 BC, followed by Carthaginians, Romans, Vandals, Visigoths and Moors. It was not until 1492, when the Catholic Monarchs got power, that Spain became a united country. Jews and Moors, who had lived happily there for many years, were driven away. Spain became very rich because it conquered many overseas countries, especially in Central and South America. However, Spain fought many wars and lost much of its treasure. It was very poor in 1936 when the Civil War was fought. General Franco became a harsh dictator until 1975 when Juan Carlos I was brought back to Spain and made king. There is now a democratic government. In prehistoric times people lived in the area which is now Madrid. The Romans lived there for several centuries. The origin of today’s city really starts in the 9th century when Muhammad I had a small palace built where the Palacio Real stands today. The Moors built strong forts in Madrid in 865 and put a wall round the city. These walls stood until 1476 when they were knocked down. In 1561 Felipe I moved the royal court from Toledo to Madrid. Madrid had now become the capital of a very large empire. Over the next years and decades the Plaza Mayor was built and many great buildings and monuments, many of which still stand. When Ferdinand II of Aragon and Queen Isabella of Castile came to Madrid, Spain had become a very rich country. The 16th and 17th centuries are now known as the “Golden Century”. Their grandson was the famous Charles I of Spain (also known as Charles V, Holy Roman Emperor). He liked his court to be in Seville. His son, Philip II (1527–1598) moved the court to Madrid in 1561. In the late 1800s there was a revolt, known as the First Spanish Republic. Later the monarchy was restored, but then there was a Second Spanish Republic followed by the Spanish Civil War. The Second Spanish Republic started on 14 April 1931 and was celebrated in La puerta del Sol which is the center of the city. Madrid suffered a lot in this war. It was bombed by airplanes. During the dictatorship of Francisco Franco, especially during the 1960s, south Madrid became very industrialized, and many people from the rural areas moved to Madrid especially to the south east of the city. When General Franco died and democracy was restored, Madrid became more prosperous. During the 1980s and 1990s many new buildings were put up. Also when Franco died, the Spanish monarchy returned. Madrid has been attacked many times by terrorists. This includes the bombing of a restaurant in 1985, killing 18 people and the of trains in 2004, killing over 190 people. == Geography == Madrid has a borderline cold semi-arid climate (BSk in the Köppen climate classification) and a hot-summer Mediterranean climate (Csa in the Köppen climate classification). Most rain falls in autumn and spring. The winters are cool because it is high up, and occasionally it snows. The summers are hot and dry. Often the temperature is above 30 °C (86 °F) in July and August and can often reach 40 °C (104 °F). At night it is much cooler. This is why many offices and businesses are closed and people have a sleep (siesta) in the afternoon when it is hot. Then they come out again in late afternoon and often eat dinner late at night. == Buildings in Madrid == Spain's Royal Palace is in Madrid. It is one of the largest palaces in all of Western Europe. But the king and his family do not live there anymore; they live in a smaller palace, and only use the Royal Palace for important events, like meeting other kings and other official ceremonies. One can go inside the Royal Palace and learn about the history of Spanish monarchy. Other famous buildings are: The Prado Museum, the Temple of Debod, the Santiago Bernabeú Stadium and the Cuatro Torres Business Area. == Art galleries == There are a lot of very big and important art museums in Madrid. The most famous ones are the Prado Museum, the Queen Sofia Museum, and the Thyssen-Bornemisza museum. These show off paintings, sculptures, and other works of art from some of the most famous artists in the world. Many famous, important, and valuable works of art are in these museums. For example, the Queen Sofia museum has a famous painting by Pablo Picasso, called Guernica. Picasso painted this painting to show how sad and angry it made him when the German Nazis destroyed a town in Spain called Guernica in 1937. Picasso had said that the painting should never return to Spain until it was a democracy again. Once that happened, they built the Queen Sofia museum just to have a good place to put it. == Other sights == There are many other sights to see in Madrid. Many people go to see the Plaza Mayor which was a market place. The Plaza de la Villa was another famous market place. There are a lot of shops along the Gran Via. Real Madrid football fans celebrate at the Plaza de Cibeles. Two famous gates to see are the Puerta del Sol and the Puerta de Alcalá. A more recent landmark is the Almudena Cathedral. Madrid has some lovely parks. The Retiro Park is the most famous. The Cristal Palace can be found in this park. The Plaza de Toros is visited by many tourists. Bullfights take place there. == References == == Other websites == * Madrid’s official tourist website * Madrid´s English on line magazine * French wiki entry explaining more about Guernica * WorldFlicks in Madrid: Photos and interesting places on Google Maps Category:Capital cities in Spain Category:9th-century establishments in Europe Category:Establishments in Spain",
            title: "Madrid"
        }
    ]


    for (const doc of all_docs) {
        index.add(doc);
    }

    let results = await index.search({
                query: "spain",
                suggest: true,
                enrich: true,
                highlight: "<b>$1</b>"
            }
        )
    ;

    console.log(results);
</script>
</body>
</html>

Thanks a lot for the work this library seems really promising!

ts-thomas added a commit that referenced this issue Mar 26, 2025
@ts-thomas
Copy link
Contributor

Fix is published. Thanks for the example.

@ts-thomas
Copy link
Contributor

The last term from ID 473 wasn't highlighted, I will push an update later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants