0

I really think that I'm trying to do is fairly simple. I'm simply trying to query for N tags. A clear example of this was asked and answered over at "Elasticsearch: How to use two different multiple matching fields?". Yet, that solution doesn't seem to work for the latest version of ES (more likely, I'm simply doing it wrong).

To show the current data and to demonstrate a working query, see below:

{
    "query": {
        "filtered": {
             "filter": { 
                 "terms": { 
                    "Price": [10,5]
                }
            }
        }
    }
}

Here are the results for this. As you can see, 5 and 10 are showing up (this demonstrates that basic queries do work):

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGMYXB5vRcKBZaDw",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Medium Signs" ],
        "Code" : "a",
        "Name" : "Sample 1",
        "Timestamp" : 1.455031083799152E9,
        "Price" : "10",
        "IsEnabled" : true
      }
    }, {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGHHXB5vRcKBZaDF",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Small Signs" ],
        "Code" : "b",
        "Name" : "Sample 2",
        "Timestamp" : 1.45503108346191E9,
        "Price" : "5",
        "IsEnabled" : true
      }
    }, {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGILXB5vRcKBZaDO",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Medium Signs" ],
        "Code" : "c",
        "Name" : "Sample 3",
        "Timestamp" : 1.455031083530215E9,
        "Price" : "10",
        "IsEnabled" : true
      }
    }, {
      "_index" : "labelsample",
      "_type" : "entry",
      "_id" : "AVLGnGGgXB5vRcKBZaDA",
      "_score" : 1.0,
      "_source" : {
        "Category" : [ "Medium Signs" ],
        "Code" : "d",
        "Name" : "Sample 4",
        "Timestamp" : 1.4550310834233E9,
        "Price" : "10",
        "IsEnabled" : true
      }
    }]
  }
}

As a side note: the following bool query gives the exact same results:

{
    "query": {
        "bool": {
            "must": [{
                "terms": {
                    "Price": [10,5]
                }
            }]
        }
    }
}

Notice Category...

Let's simply copy/paste Category into a query:

{
    "query": {
        "filtered": {
             "filter": { 
                 "terms": { 
                    "Category" : [ "Medium Signs" ]
                }
            }
        }
    }
}

This gives the following gem:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

Again, here's the bool query version that gives the same 0-hit result:

{
    "query": {
        "bool": {
            "must": [{
                "terms": {
                    "Category" : [ "Medium Signs" ]
                }
            }]
        }
    }
}

In the end, I definitely need something similar to "Category" : [ "Medium Signs", "Small Signs" ] working (in concert with other label queries and minimum_should_match as well-- but I can't even get this bare-bones query to work).

I have zero clue why this is. I poured over the docs for houring, trying everything I can see. Do I need to look into debugging various encodings? Is my syntax archaic?

2
  • Isn't because your Category field already is an array? Commented Feb 9, 2016 at 16:10
  • can you please update the question with mapping? Commented Feb 9, 2016 at 16:28

1 Answer 1

1

The problem here is that ElasticSearch is analyzing and betokening the Category field, and the terms filter expects an exact match. One solution here is to add a raw field to Category inside your entry mapping:

PUT labelsample
{
  "mappings": {
    "entry": {
      "properties": {
        "Category": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "Code": {
          "type": "string"
        },
        "Name": {
          "type": "string"
        },
        "Timestamp": {
          "type": "date",
          "format": "epoch_millis"
        },
        "Price": {
          "type": "string"
        },
        "IsEnabled": {
          "type": "boolean"
        }
      }
    }
  }
}

...and filter on the raw field:

GET labelsample/entry/_search
{
    "query": {
        "filtered": {
             "filter": { 
                 "terms": { 
                    "Category.raw" : [ "Medium Signs" ]
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yikes! I can't believe I missed that. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.