Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

from enum import unique
from itertools import groupby
from unicodedata import category

from vespa.package import Field, RankProfile, MatchPhaseRanking
%load_ext autoreload
%autoreload 2
import mycode.vap as vap
# Index about 10k docs
# have 10 categories
# Score random in the first phase
# match-phase-should take top1 group
# search for top-10 hits
#
app = vap.demo_application_package()
from vespa.package import Field, Diversity, MatchPhaseRanking, RankProfile

app.get_schema().document.add_fields(
    Field(name="category", type="string", indexing="attribute | summary", attribute=["fast-search"])
)
app.get_schema().add_rank_profile(
    RankProfile(
        name="matchphase",
        match_phase=MatchPhaseRanking(
            attribute="id",
            order="descending",
            max_hits=10
        ),
        first_phase="attribute(id)",
    ),
)
app.get_schema().add_rank_profile(
    RankProfile(
        name="matchphasediversity",
        match_phase=MatchPhaseRanking(
            attribute="id",
            order="descending",
            max_hits=10
        ),
        diversity=Diversity(
            attribute="category",
            min_groups=10,
        ),
        first_phase="random",
    ),
)
print(app.get_schema().schema_to_text)
schema doc {
    document doc {
        field id type int {
            indexing: attribute
            attribute {
                fast-search
            }
        }
        field category type string {
            indexing: attribute | summary
            attribute {
                fast-search
            }
        }
    }
    rank-profile fields inherits unranked {
        function id() {
            expression {
                attribute(id)
            }
        }
        first-phase {
            expression {
                0
            }
        }
        summary-features {
            id
        }
        match-features {
            id
        }
    }
    rank-profile matchphasediversity {
        diversity {
            attribute: category
            min-groups: 10
        }
        match-phase {
            attribute: id
            order: descending
            max-hits: 10
        }
        first-phase {
            expression {
                random
            }
        }
    }
    rank-profile matchphase {
        match-phase {
            attribute: id
            order: descending
            max-hits: 10
        }
        first-phase {
            expression {
                0
            }
        }
    }
}
from vespa.deployment import VespaDocker

# In case running colima on macos run the following
# !sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
vespa_docker = VespaDocker(
    container_image="vespaengine/vespa:8.650.16",
)
# Start a docker container and deploy the application package
client = vespa_docker.deploy(
    application_package=app,
)
Waiting for configuration server, 0/60 seconds...
Waiting for configuration server, 5/60 seconds...
Application is up!
Finished deployment.
vap.feed(client, docs=[{'id': index, 'category': int(index / 1000)} for index in range(10000)])
resp1 = client.query(body={
    'yql': 'select * from sources * where true',
    'hits': 400,
    'ranking.profile': 'matchphasediversity',
}).json
resp1['root']['children']
resp1
from collections import Counter
Counter(list(map(lambda x: x['fields']['category'], resp1['root']['children'])))
Counter({'0': 140, '3': 12, '8': 12, '2': 12, '7': 12, '6': 12, '4': 12, '5': 12, '1': 12, '9': 12})
resp = client.query(body={
    'yql': 'select * from sources * where true',
    'hits': 400,
    'ranking.profile': 'matchphase',
    # 'ranking.matchPhase.diversity.minGroups': 0,
    # 'ranking.matchPhase.diversity.attribute': "id",
}).json
resp
{'root': {'children': [{'fields': {'category': '9', 'documentid': 'id:doc:doc::9999', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9999', 'relevance': 9999.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9998', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9998', 'relevance': 9998.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9997', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9997', 'relevance': 9997.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9996', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9996', 'relevance': 9996.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9995', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9995', 'relevance': 9995.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9994', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9994', 'relevance': 9994.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9993', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9993', 'relevance': 9993.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9992', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9992', 'relevance': 9992.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9991', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9991', 'relevance': 9991.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9990', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9990', 'relevance': 9990.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9989', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9989', 'relevance': 9989.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9988', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9988', 'relevance': 9988.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9987', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9987', 'relevance': 9987.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9986', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9986', 'relevance': 9986.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9985', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9985', 'relevance': 9985.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9984', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9984', 'relevance': 9984.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9983', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9983', 'relevance': 9983.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9982', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9982', 'relevance': 9982.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9981', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9981', 'relevance': 9981.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9980', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9980', 'relevance': 9980.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9979', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9979', 'relevance': 9979.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9978', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9978', 'relevance': 9978.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9977', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9977', 'relevance': 9977.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9976', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9976', 'relevance': 9976.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9975', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9975', 'relevance': 9975.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9974', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9974', 'relevance': 9974.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9973', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9973', 'relevance': 9973.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9972', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9972', 'relevance': 9972.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9971', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9971', 'relevance': 9971.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9970', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9970', 'relevance': 9970.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9969', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9969', 'relevance': 9969.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9968', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9968', 'relevance': 9968.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9967', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9967', 'relevance': 9967.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9966', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9966', 'relevance': 9966.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9965', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9965', 'relevance': 9965.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9964', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9964', 'relevance': 9964.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9963', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9963', 'relevance': 9963.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9962', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9962', 'relevance': 9962.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9961', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9961', 'relevance': 9961.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9960', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9960', 'relevance': 9960.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9959', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9959', 'relevance': 9959.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9958', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9958', 'relevance': 9958.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9957', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9957', 'relevance': 9957.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9956', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9956', 'relevance': 9956.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9955', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9955', 'relevance': 9955.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9954', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9954', 'relevance': 9954.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9953', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9953', 'relevance': 9953.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9952', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9952', 'relevance': 9952.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9951', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9951', 'relevance': 9951.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9950', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9950', 'relevance': 9950.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9949', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9949', 'relevance': 9949.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9948', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9948', 'relevance': 9948.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9947', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9947', 'relevance': 9947.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9946', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9946', 'relevance': 9946.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9945', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9945', 'relevance': 9945.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9944', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9944', 'relevance': 9944.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9943', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9943', 'relevance': 9943.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9942', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9942', 'relevance': 9942.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9941', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9941', 'relevance': 9941.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9940', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9940', 'relevance': 9940.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9939', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9939', 'relevance': 9939.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9938', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9938', 'relevance': 9938.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9937', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9937', 'relevance': 9937.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9936', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9936', 'relevance': 9936.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9935', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9935', 'relevance': 9935.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9934', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9934', 'relevance': 9934.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9933', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9933', 'relevance': 9933.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9932', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9932', 'relevance': 9932.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9931', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9931', 'relevance': 9931.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9930', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9930', 'relevance': 9930.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9929', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9929', 'relevance': 9929.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9928', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9928', 'relevance': 9928.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9927', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9927', 'relevance': 9927.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9926', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9926', 'relevance': 9926.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9925', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9925', 'relevance': 9925.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9924', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9924', 'relevance': 9924.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9923', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9923', 'relevance': 9923.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9922', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9922', 'relevance': 9922.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9921', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9921', 'relevance': 9921.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9920', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9920', 'relevance': 9920.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9919', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9919', 'relevance': 9919.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9918', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9918', 'relevance': 9918.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9917', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9917', 'relevance': 9917.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9916', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9916', 'relevance': 9916.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9915', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9915', 'relevance': 9915.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9914', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9914', 'relevance': 9914.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9913', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9913', 'relevance': 9913.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9912', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9912', 'relevance': 9912.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9911', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9911', 'relevance': 9911.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9910', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9910', 'relevance': 9910.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9909', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9909', 'relevance': 9909.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9908', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9908', 'relevance': 9908.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9907', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9907', 'relevance': 9907.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9906', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9906', 'relevance': 9906.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9905', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9905', 'relevance': 9905.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9904', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9904', 'relevance': 9904.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9903', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9903', 'relevance': 9903.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9902', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9902', 'relevance': 9902.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9901', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9901', 'relevance': 9901.0, 'source': 'test_content'}, {'fields': {'category': '9', 'documentid': 'id:doc:doc::9900', 'sddocname': 'doc'}, 'id': 'id:doc:doc::9900', 'relevance': 9900.0, 'source': 'test_content'}], 'coverage': {'coverage': 3, 'degraded': {'adaptive-timeout': False, 'match-phase': True, 'non-ideal-state': False, 'timeout': False}, 'documents': 254, 'full': False, 'nodes': 1, 'results': 1, 'resultsFull': 0}, 'fields': {'totalCount': 256}, 'id': 'toplevel', 'relevance': 1.0}}
Counter(list(map(lambda x: x['fields']['category'], resp['root']['children'])))
Counter({'9': 128, '0': 128})