How to select distinct field values using Solr?

前端 未结 6 1068
别跟我提以往
别跟我提以往 2020-11-28 04:01

I would like to do the equivalent of this SQL but with Solr as my data store.

SELECT
   DISTINCT txt
FROM
   my_table;

What syntax would fo

6条回答
  •  旧巷少年郎
    2020-11-28 04:54

    Use the StatsComponent with parameter stats.calcdistinct to get a list of distinct values for a certain field:

    Solr 7 https://lucene.apache.org/solr/guide/7_7/the-stats-component.html

    Solr 6 https://cwiki.apache.org/confluence/display/solr/The+Stats+Component

    It will also give you the count of distinct values. stats.calcdistinct is probably available since 4.7.

    http://wiki.apache.org/solr/StatsComponent is outdated as it does not cover stats.calcdistinct

    Example

    /select?stats=on&stats.field=region&rows=0&stats.calcdistinct=true
    
    "stats":{
      "stats_fields":{
        "region":{
          "min":"GB",
          "max":"GB",
          "count":20276,
          "missing":0,
          "distinctValues":["GB"],
          "countDistinct":1}}}}
    

    Difference to Facets

    In case of facets you need to know the count to request all, or you set the facet.limit to something really high and count the result yourself. Also, you need a string field for making facets work the way you need it here.

提交回复
热议问题