Search code examples
elasticsearchelasticsearch-percolate

How to create percolate index in Elasticsearch by multiple fields


I have an index: products

curl -XPUT "http://localhost:9200/products/product/1" -d'
{
    "title": "Iphone 6",
    "category_id": 4,
    "price": 7788,
    "owner_id": 21
}'

How to create percolate index for 3 fields: title, category_id, price?

Percolate Query here I only found how to do for 1 field.


Solution

  • Create an index with two mappings

    curl -XPUT 'localhost:9200/percolate-index?pretty' -H 'Content-Type: application/json' -d'
    {
        "mappings": {
            "doctype": {
                "properties": {
                    "title": {
                        "type": "text"
                    },
                    "category_id": {
                        "type": "long"
                    },
                    "price": {
                        "type": "long"
                    }
                }
            },
            "queries": {
                "properties": {
                    "query": {
                        "type": "percolator"
                    }
                }
            }
        }
    }'
    

    Register a query in the percolator

    curl -XPUT 'localhost:9200/percolate-index/queries/1?refresh&pretty' -H 'Content-Type: application/json' -d'
    {
        "query": {
            "bool": {
                "must": {
                    "multi_match": {
                        "query": "iphone",
                        "fields": ["title"]
                    }
                },
                "filter": [
                    {
                        "terms": { 
                            "category_id": [4]
                        }
                    }
                ]
            }
        }
    }'
    

    Match a document to the registered percolator queries

    curl -XGET 'localhost:9200/percolate-index/_search?pretty' -H 'Content-Type: application/json' -d'
    {
        "query" : {
            "percolate" : {
                "field" : "query",
                "document_type" : "doctype",
                "document" : {
                    "title" : "iphone 6",
                    "category_id" : 4
                }
            }
        }
    }'