The former was established by Turkic Qarluk-Uygur princes who converted to Islam and held Turkestan in the 11th century. Prior to the Qarluk-Uygurs' migration to Turkestan, the great Uygur Khanate of Mongolia, with its Manichaean state religion, its capital in Karabalgasun in northern central Mongolia and its vibrant Sogdian-Chinese hybrid high culture, was destroyed by Khakas, or Kyrgyz nomads from the Baikal region.
A branch of the Uygurs migrated to oasis settlements of Tarim Basin and Gansu, such as Gaochang (Khoja) and Hami (Kumul) and set up a confederation of decentralized Buddhist states called Kara-Khoja. Others, occupying western Tarim Basin, Ferghana Valley, Jugaria and parts of Kazakhstan bordering the Muslim, Turco-Tajik Khwarazm Sultanate, converted to Islam no later than 10th century and built a federation with Muslim institutions called Kara-Khanlik, whose princely dynasties are called Kara-Khanids by historians.
After the rise of the Seljuks in Iran, the Kara-Khanids became nominal vassals of the Seljuks. Later they would serve the dual suzerainty of both the Kara-Khitans to the north and the Seljuks to the south.
Influential Kara-Khanid rulers include Mahmoud Tamgach of Kashgar. After the defeat of the Khitan dynasty by the Jin Dynasty (1115-1234) in Northern China, the great Khitan mandarin Yelu Dashi escaped from China with a small band of Khitan soldiers, recruited warriors from Tangut, Tibetan, Qarluk, Karakhoja, Naiman areas and marched westward in search of asylum.
Yelu Dashi was accommodated by the hospitable Tangut Western Xia Kingdom and the Buddhist Karakhojas. However, he was shut out by the Muslim Kara-Khanids near Gulja and Kashgar. Enraged, he subjugated Karakhanid states one by one and set up the Kara-Khitan suzereignty in Balasaghun on the Irtysh river, modern day Kazakhstan.
The Kara-Khitan Khanate, though harsh on the Muslim Qarluk-Uygurs, did not dispossess all of the Kara-Khanid domains. Instead, the "Khitans" (most of them were actually Naimans, Tanguts and Qarluks speaking the same Turkic language as that of the Kara-Khanids) retreated to the northern steppes and had the Kara-Khanids act as its tax-collectors and administrators on Muslim sedentary populations (the same practice was adopted by the Golden Horde on the Russian Steppes). The Kara-Khitans even incorporated Kara-Khanid Muslim generals such as Muhammad Tai, who surrendered to the Naiman usurper Kushluk at the end of the Kara-Khitan Dynasty. Kushlug, the last ruler of the Kara-Khitan Dynasty, was especially harsh on the Muslim populations under his suzereignty. He went so far as to forcing conversions from Islam to Buddhism, the dominant religion of the ruling Kara-Khitans. The elite Kara-Khitans and their Naiman soldiers, on an interesting note, are very often Nestorian Christians, as suggested by the Syriac names of the Gur-Khans(Emperors), who at the same time had confucian titles and patronized Buddhist establishments. Kushluk's Naimans were perhaps heavily Nestorian Christian. The reason for force conversions into Buddhism was perhaps due to the underdevelopment of Nestorian institutions, making it unsuitable on sedentary domination. The "Christian" Kara-Khitan yoke on the Muslim Kara-Khanids also gave rise to the myth of Prestor John, who was supposed to vanquish the menace of Islamadom to the wishes of European Christendom.
Several military commanders of Kara-Khanid lineages such as the father of Osman of Khwarezm, escaped Kara-Khanid lands during the Kara-Khitan invasion. In 1244, upon the invitation of the Egyptian Mamluks, Osman of Khwarezm marched on Jerusalem and liberated the holy city, on behalf of Islam, from the Crusaders.
Kara-Khanid legacy is arguably, the most enduring cultural heritage among coexisting cultures in Central Asia from 9-13th century. The Qarluk-Uygur dialect spoken by the nomadic hordes and Turkified sedentary populations under Kara-Khanid rule branched out into two major branches of the Turkic linguistic family, namely, the Chagatay and the Kypchak. The Kara-Khanid cultural model that combined nomadic Turkic culture with Islamic, sedentary institutions spread east into former Kara-Khoja and Tangut territories, and west and south into the Subcontinent, Khurasan (Turkmenistan, Afghanistan and Northern Iran), Golden Horde territories (Tataristan) and Turkey. The Mongol Chagatay, Timurid and Uzbek states and societies inherited the bulk of the cultures of the Kara-Khanids and the Khwarazmians without much interruption. It is perhaps because of the similarities between Kara-Khanid and Kara-Khoja cultures that during Yuan and Ming periods former Kara-Khoja and Xixia lands were populated by converts to Islam indistinguishable from Chagatay and Timurid lands. These Turkic Muslims under Chinese influence later adopted the Chinese language while still maintaining extensive trade relations with Turkestan. They were designated "Hui" in Chinese, obviously derived from "Huihui" or "Huihu", an archaic transliteration of "Uygur". The Kara-Khanid culture started as a literate tradition, with a body of Muslim subjects recorded in the vertical Sogdian script of the first Uygur Empire. Despite continuity from the first Uygur Empire and affinity with the Kara-Khojas, the Kara-Khanids claimed descent from the legendary Persian Afrasiab dynasty. The use of the vertical Uygur script among Muslim Turks extended well into Timurid times in western Turkistan, and well into Manchu times in some enclaves in eastern Turkistan. The Anatolian Turkish beyliks in IlKhanid times and early Ottoman times still retained scribes trained in the vertical script in order to do transactions with the Timurids. These scribes were called "bakshy", a name possibly of Chinese origin, meaning "great scholar", one of the titles of the Confucian soldier-scholar Yelu Dashi; or of Sanskrit origin. The nomadic elements of the Kara-Khanid and Kara-Khitan states, the Qarluk and Naiman hordes, laid foundation for the modern Kypchak Turkic-speaking cultures of the Kazaks, Kyrgyz and Tatars. The Muslim, Persianized, sedentary elements of the Kara-Khanid culture is preserved today among the Tajik, Uzbek, Afghan, Hui and Uygur nations, two of which speak Chagatay Turkic languages.
The Islamized Qarluk princely clan, the Balasaghunlu Ashinalar (the Karakhanids) gravitated toward the Persian Islamic cultural zone after their political autonomy and suzereignty over Central Asia was secured during the 9-10th century.
As they became increasingly Persianized (to the point of adopting "Afrasiab", a Shahnameh mythical figure as the ancestor of their lineage), they settled in the more Indo-Iranian sedentary centers such as Qashgari, and became detached from the nomadic traditions of fellow Qarluqs, many of whom retained the Nestorian-Mahayana-Manichaean religious mixture of the former Uyghur Khanate.
When the Khitays came, along with Nestorian Naiman recruits, they solicited support among Qarluks from Semirechye to the Irtysh region.
Though largely Mahayana and Confucian in organizing principles, the nucleus of Khitay elites were wise to adopt elements of Nestorian identity, as reflected in the Christian names of the Kara-Khitay Gur-Khans.
The Khitay conquest of Central Asia, can thus be seen as internecine struggle within the Qarluk nomadic tribe, only played out as dynastic conflict between the conquering Khitay elites and the defending Kara-Khanid princes, resulting in the subjugation of the latter by the former, and in the subjugation of the Muslim Qarluks by their Nestorian kins and the Nestorian Naimans.