Elasticsearch or match query

I am trying to write a query to find products on two columns named category1

and category2

. I am working using an elastic search php client and am trying to execute a match query, but it gives me incorrect results due to substring matching.

But I am looking for an exact OR match on two columns. I'm new to this, please help me.

    $params['index'] = 'furnit';
    $params['type']  = 'products';
    $params['body']['query']['bool']['should'] = array(
        array('match' => array('category1' => $category->name)),
        array('match' => array('category2' => $category->name)),
    );

    $results = $this->elasticsearch->search($params);

      

+3


source to share


2 answers


If you are not doing a search, then using a bool query in this scenario is the wrong way to do it in elasticsearch. Queries are used when you are looking for something and search keyword relevance and document matching score.

Here you can apply the bool elasticsearch filter to filter the results you want. Using filters with queries (filtered query) is the correct way to do it as it excludes all non-matching documents and then you can search for the documents you want using matches.

here is an example of a bool filter



{
    "from": 0,
    "size": 50,
    "sort": [
        {
            "name" : {
                "order": "asc" 
            }
        }
    ],
    "query": {
        "filtered": {
            "query": {
                "match_all" : {} 
            },
            "filter": {
                "bool": { 
                    "should": [
                        {
                            "term": {
                                "category1" : "category1"  
                            }
                        },
                        {
                            "term": {
                                "category2" : "category2"
                            }
                        }
                    ]
                }
            }
        }
    }
}

      

you can also refer to the docs ( https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html )

0


source


Perhaps your problem is that you were using the default analyzer (which is the standard analyzer). could you give me your mapping? I suggest you change your use of not_analyzer when indexing and using the term filter / query.

You can use put collation here to customize your parser: Put collation



Edit: I created the gist for you, check here:

Matching and Condition Filters

0


source







All Articles