How to select distinct field values using Solr?

前端 未结 6 1067
别跟我提以往
别跟我提以往 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:56

    Solr 5.1 and later has the new Facet Module that has integrated support for finding the number of unique values in a field. You can even find the number of unique values in a field for each bucket of a facet, and sort by that value to find the highest or lowest number of unique values.

    Number of unique values in "myfield": json.facet={x:'unique(myfield)'}

    Facet by "category" field, and for each category, show the number of unique values in "color":

    json.facet={
      cat_breakdown : { terms : {  // group results by unique values of "category"
        field : category,
        facet : {
          x : "unique(color)",  // for each category, find the number of unique colors
          y : "avg(price)"      // for each category, find the average price
        }
      }}
    }
    

    This is in Solr 5.1 and later. More facet functions like "unique" are shown at http://yonik.com/solr-facet-functions/

提交回复
热议问题