Solr query search for multiple instances for singl

2019-05-06 15:35发布

问题:

I'm stuck on this one issue. What i want to do is to query on a Multivalued and see if a value comes up at least try. For example the field must be "FREE","FREE" and not just "FREE" or "FREE","IN_USE".

Field 
<field name="point_statusses" type="string" indexed="true" stored="true" multiValued="true" />

Type
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />

SQL
GROUP_CONCAT(cp.status) as point_statusses

Clarification:

I have an object that has multiple plugs and those all have a status of FREE, IN_USE or ERROR. What i want to do is filter on ones that have two plugs with status FREE and I can't change the structure of the schema.xml. How do i query to for this?

回答1:

Unfortunately, it cannot be done without applying any changes to schema, because solr.StrField does not preserve term frequency information.

Quote from schema.xml:

  ...
  1.2: omitTermFreqAndPositions attribute introduced, true by default 
        except for text fields.
  ...

However, if you can apply some changes, then the following will work (tested on the Solr 4.5.1):

1) Make one of the following changes to schema:

  • Change field to text_general (or any solr.TextField field);
<field name="point_statusses" type="text_general" indexed="true" stored="true" multiValued="true" />
  • OR add omitTermFreqAndPositions="false" to point_statusses definition:
<field name="point_statusses" type="string" indexed="true" stored="true" multiValued="true" omitTermFreqAndPositions="false"/>

2) Filter by term frequency. Examples:

Search documents having exactly 2 'FREE' point_statusses:

{!frange l=2 u=2}termfreq(point_statusses,'FREE')

Or from 2 to 3 'FREE' point_statusses:

{!frange l=2 u=3}termfreq(point_statusses,'FREE')

The final solr query may look like this:

http://localhost:8983/solr/stack20746538/select?q=*:*&fq={!frange l=2 u=3}termfreq(point_statusses,'FREE')