Making a compound $search query with multiple attribute matching

I would like to be able to make a search query using the compound pipeline. An example of a document in the collection I am querying is below:
{
title: “Nike Flyknit Pegasus 32”
gender: “Women”
category: “Shoes”
description: “Newest Nike running shoe with Zoom foam in the forefoot”
brand: “Nike”
price: 120
}

I have a search index on “title”, and want to be able to perform a search where I filter based on “gender”, “category”, “brand”, and < price, but it seems I cannot stack filters in a must case?

I want the query to filter by the fields (similar to a match) so if I make a search for “Adidas Ultraboost” with filters “Women” “Shoes” and < $ 150, I want to search the “title” index with “Adidas Ultraboost”, but also only show results that match “Women” “Shoes” and are less than 150 for the price attribute.

Any advice on how to make the compound pipeline work would be appreciated.

You should be able to something like this:

 $search: {
            "compound": {
                    "must": [
                        {
                            "text": {
                                "path": "title",
                                "query": "Nike Flyknit Pegasus 32",
                            }
                        },
                        {
                            "text": {
                                "path": "gender",
                                "query": "Women",
                            }
                        },
                    ]
            }
        },
    },

Thanks for the example query. I was running the compound stage like that however it seems like “must” isn’t enforced strictly? For something like gender or category I am looking for a strict match. I only want to search for results that absolutely have the attribute Women or category Shoes.

Try using the phrase operator instead of text.

I am new to Mongo as well :slight_smile:

{
	"$search": {
        "compound": {
                
            "must": [{
                "phrase": {
                    "query": "Shoes",
                    "path": "category"
                },
                "phrase": {
                    "query": "Nike",
                    "path": "brand"
                }
                }],
            
            },
            
	},
  },
  { "$project" : { "_id" : 0 } },
  { "$limit" : 24 }
])

Even with a pipeline like this, the search results show documents that do not match 1 of the 2 must cases. Do I need to combine the all phrase statements in the must clause into a singular one?

Which analyzer is the index using? I believe you should be using the Keyword Analyzer.

Currently I only have a single index (dynamic over index fields) that is using lucene.standard for the index analyzer and lucene.standard for the search analyzer. Do I need to make a separate index for each field I want to query with a keyword analyzer? And should the lucene option be consistent across index analyzer and search analyzer?

I am pretty sure using different analyzers for indexing and searching would lead to different behaviors. It would be mostly trial and error at this point. I will be doing these experiments over the next few weeks.

At this point, it is one blind guiding the other :slight_smile:

Makes sense, I will experiment with some different analyzers and see if I can get this query working. Thanks!