Exact search in array object type using elasticsearch

后端 未结 2 1934
深忆病人
深忆病人 2021-02-04 04:58

I\'m looking for a way to do exact array matches in elastic search. Let\'s say these are my documents:

{\"id\": 1, \"categories\" : [\"c\", \"d\         


        
2条回答
  •  你的背包
    2021-02-04 05:41

    If you have a discrete, known set of categories, you could use a bool query:

    "bool" : {
        "must" : {
            "terms" : { "categories" : ["c", "d"],
                 minimum_should_match : 2
             }
        },
        "must_not" : {
            "terms" : { "categories" : ["a", "b", "e"],
                 minimum_should_match : 1
             }
        }
    }
    

    Otherwise, Probably the easiest way to accomplish this, I think, is to store another field serving as a categories keyword.

    {"id": 1, "categories" : ["c", "d"], "categorieskey" : "cd"}
    

    Something like that. Then you could easily query with a term query for precisely the results you want, like:

    term { "categorieskey" : "cd" }
    

    And you could still search non-exclusively, as;

    term { "categories" : "c" }
    

    Querying for two categories that must both be present is easy enough, but then preventing any other potential categories from being present is a bit harder. You could do it, probably. You'dd probably want to write a query to find records with both, then apply a filter to it eliminating any records with categories other than the ones specified. It's not really a sort of search that Lucene is really designed to handle, to my knowledge.

    Honestly I'm having a bit of trouble coming up with a good filter to use here. You might need a script filter, or you could filter the results after they have been retrieved.

提交回复
热议问题