Storing and searching dynamic key value pairs in S

2019-07-27 23:07发布

问题:

I need to store and index key value pairs attached with a particular Solr document. For eg, there might be a variable number of tags(labels) attached to a solr document with each tag name having an integer along with it as a value for it.

   <doc>
     <id>1001</id>
     #dymanic tags attached
     php=>10
     web=>2
     programming=>12
     mysql=>10

    </doc>  

So when I search for "php" this document should showup in the search results and I should be able to read its value for that particular document (in case of example its value is 10). How do I create a schema for this and how do i store this. Also how do I search for it using the REST api?

回答1:

With dynamic fields, you would need to add them as a key for getting the count and as well as values for making them searchable.

Instead you can try -

Add them as the multivalued field for both the languages and the counts.

The language field will hold the values of the languages, and can also serve as the searchable fields.

<field name="language" type="text" indexed="true" stored="true" multiValued="true"/>

Add the language count as the other multivalued fields, with the maintained order.

<field name="language_count" type="int" indexed="false" stored="true" multiValued="true"/>

In the response, you would have two arrays for language and language_counts and you can map the count for the language on the client side by the position.



回答2:

Updating a bit because solr has new features:

You're in XML, and solr is good at xml, so use XML, then let solr do its own parsing.

In other words, you might consider one of these:

<doc>
  <id php="10" web="2" programming="12" mysql="10">1001</id>
</doc>

or

<doc>
  <element id="1001" php="10" web="2" programming="12" mysql="10">
    #dymanic tags attached
    php=>10
    web=>2
    programming=>12
    mysql=>10
  </element>
</doc>

or

<doc>
  <element>
    <id>1001</id>
    <php>10</php>
    <web>2</web>
    <programming>12</programming>
    <mysql>10</mysql>
  </element>
</doc>

Of course, if you REALLY can't change your data itself, the original answer still stands.