Home > Blockchain >  Query an array that contain string with terms
Query an array that contain string with terms

Time:10-27

I want filter result from Elasticsearch where array of strings contain one of specific string value:

This is current json result, no filtered :

{
            "id": 1,
            "title": "Title",
            "tags": [
                "tag filter"
            ],
},
{
            "id": 2,
            "title": "Title",
            "tags": [
                "different tag filter"
            ],
},

Objectif : obtain only result where tags contain simple "tag filter" What i made:

 $termsQuery = new Terms('POITypes');
 $termsQuery->setTerms($terms); // $terms => array("tag filter")
 $boolQuery->addMust($termsQuery);

What dd report:

SearchController.php on line 90:
Elastica\Query\BoolQuery {#892
  #_params: array:1 [
    "must" => array:1 [
      0 => Elastica\Query\Terms {#893
        #_params: array:1 [
          "tags" => array:1 [
            0 => "tag filter"
          ]
        ]
        #_rawParams: []
        -field: "tags"
      }
    ]
  ]
  #_rawParams: []
}

When i add this must filter with this terms requirement, there are 0 result obtain. I found no solution since 2 days for this simple query... what's wrong with my code ?

Expecting only result where tags array includes one of my terms values.

CodePudding user response:

Pass field name as tags.keyword.

Tags field is of type "text" so your query is trying to match "tag filter"against individual tokens["tag","filter"], you need to match it against full value.

If index was auto created , it will have keyword subfield by default, otherwise you will need to create it.

  • Related