heinob
heinob

Reputation: 19474

Query specificly indexed value in multivalued field

I have a multivalued field which is filled by an array of strings. Now I want to find all documents that have i. e. foo as the i. e. second (!) string in this field. Is this possible?

If it is not, what would be your recommendation to achieve this?

Upvotes: 1

Views: 900

Answers (2)

Alexandre Rafalovitch
Alexandre Rafalovitch

Reputation: 9789

For Solr, you can use UpdateRequestProcessor to copy and modify the field to add position prefix. So, you'll end up with 2_91 or similar. You can use StatelessScriptURP for that.

Alternatively, you could send this information as multiple fields and have dynamic field definition to map them.

Basically, for both Solr and ES, underlying Lucene stores multivalued strings as just one long string with large token offset between last token of first value and first token of second value. So, absolute positions require some sort of hack. Runtime hacks (e.g. ElasticSearch example in the other answer) are expensive during query. Content modifying hacks (e.g. URP in this example) are expensive with additional disk space or with more complex schema.

Upvotes: 1

progrrammer
progrrammer

Reputation: 4489

In elasticsearch, you can achieve this using Script Filter, Here is a sample,

consider a mapping for phone_no as,

{
   "index": {
      "mappings": {
         "type": {
            "properties": {
               "phone_no": {
                  "type": "string"
               }
            }
         }
      }
   }
}

put a document (first),

POST index/type
{
    "phone_no" :["91","92210"]
}

and second one too,

POST index/type
{
    "phone_no" :["92210","91"]
}

so, if you want to find the second value equals 91, then here is a query,

POST index/type/_search
{
    "filter" :{
        "script": {
           "script": "_source.phone_no[1].equals(val)",
           "params": {
               "val" :"91"
           }
        }
    }
}

where , val can be user defined,

Here in the above script, no case is handled (like, if it have size >1 , which may return execption sometime, you can modify script for your need ). Thanks,

Hope this might helps!!

Upvotes: 0

Related Questions