How to filter out elements from an array that doesn't match the query?


(Alexander Skiller) #1

Hi dear elasticsearch community,

Here is my index

POST /products
{
   "settings": {
      "analysis": {
         "filter": {
            "nGram_filter": {
               "type": "nGram",
               "min_gram": 2,
               "max_gram": 20,
               "token_chars": [
                  "letter",
                  "digit",
                  "punctuation",
                  "symbol"
               ]
            }
         },
         "analyzer": {
            "nGram_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding",
                  "nGram_filter"
               ]
            }
         }
      }
   }
}

here is my mapping

POST /products/paramSuggestions/_mapping
{
   "properties": {
       "productName": {
          "type": "string"
       },
   "params": {
          "properties": {
             "paramName": {
                "type": "string",
                "analyzer": "nGram_analyzer"
             },
             "value": {
                "type": "string",
                "analyzer": "nGram_analyzer"
             }
          }
       }
   }
}

here is a sample product

POST /products/paramSuggestions/1
{
  "productName": "iphone 6",
  "params": [
    {
      "paramName": "color",
      "value": "white"
    },
    {
      "paramName": "capacity",
      "value": "32GB"
    }
  ]
}

here is a sample query to get the document by matching ngram parameter name

GET products/paramSuggestions/_search
{
   "size": 10,
   "query": {
      "filtered": {
         "query": {
            "match": {
               "paramName": {
                  "query": "col",
                  "operator": "and"
               }
            }
         }
      }
   }
}

here is the unwanted result of previous query

{

   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.33217794,
      "hits": [
         {
            "_index": "products",
            "_type": "paramSuggestions",
            "_id": "1",
            "_score": 0.33217794,
            "_source": {
               "productName": "iphone 6",
               "params": [
                  {
                     "paramName": "color",
                     "value": "white"
                  },
                  {
                     "paramName": "capacity",
                     "value": "32GB"
                  }
               ]
            }
         }
      ]
   }
}

here is the wanted result:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.33217794,
      "hits": [
         {
            "_index": "products",
            "_type": "paramSuggestions",
            "_id": "1",
            "_score": 0.33217794,
            "_source": {
               "productName": "iphone 6",
               "params": [
                  {
                     "paramName": "color",
                     "value": "white"
                  },
               ]
            }
         }
      ]
   }
}

So the question is, how should the GET query look like, so it returns the WANTED query?

Thank you very much for your time :wink:


(system) #2