How to query against `stringFacet`

I have an index that defines multiple collections of stringFacet. For an example, let’s say we have the following index:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "colorsOfTheRainbow": {
        "type": "stringFacet"
      }
    }
  }
}

How would one construct a query to get all of the documents that have colorOfTheRainbow === 'red', for example?

I tried using a regular text operator, but that didn’t yield any results. A comparable query against this definition would look like this:

$search: {
  index: 'arbitrary',
  text: {
    path: 'colorsOfTheRainbow',
    query: 'red'
  }
}

The workaround that I haven’t tried, but suspect will work would be to index the value twice, once as a stringFacet and once as a string with the lucene.keyword analyzer.

Is this the expected approach?

Hey there, I would recommend trying a $searchMeta query – see the docs here.

Having the same issue. I have fields that I both need to query against and create facets for. Any work arounds for this?

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "colorsOfTheRainbow": {
        "type": ["stringFacet", "string"]
      }, 
    }
  }
}

Does something like this work?

(edited to fix syntax)

The 2nd block is invalid JSON. @Elle_Shwer if there is a way to have two types (ie stringFacet and string) for the same field, please let us know!

Sorry, I went off recall instead of precisely checking. Updated the code above, it should work.

I got this error on reindexing: Your index could not be built: “type” must be a string

Alright, well this is embarrassing but I finally believe I have the right syntax, I tested it and ran a query with it and it worked.

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "title": [
        {
          "analyzer": "lucene.english",
          "type": "string"
        },
        {
          "type": "stringFacet"
        }
      ]
    }
  }
}
1 Like

Thanks @Elle_Shwer! That did the trick!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.