Elasticsearch或匹配查询

I am trying to write a query to search for a products on two columns called category1 and category2. I am working using elastic search php client and tried with match should query but this giving me wrong results because of match of substring.

But i am looking for exact match with OR operation on two columns. I am new to this please guide 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);

If you are not searching then using a bool query in this scenario is not the right way to do it in elasticsearch. Queries are used when you are searching something and relevancy of your search keyword and score of matching documents matters.

Here you can apply a bool filter of elasticsearch to filter out the desired results. Using filters with queries (filtered query) is right way to do it as it excludes all non-matching documents and then you can search for desired documents by using match queries.

here's 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 refer to docs as well (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html)

Maybe your problem is you have used default analyzer (which is standard analyzer). could you give me your mapping ? I suggest you to change to use not_analyzer when indexing and use term filter/query.

You could use put mapping here to setting for your analyzer: Put Mapping

Edit: I have created a gist for you, check it here:

Mappings & Terms Filter