soulkphp
soulkphp

Reputation: 3833

How to filter fields in ElasticSearch using GET

I recently installed ElasticSearch with the Wikipedia river because I'm working on an autocomplete box with article titles. I have been trying to figure out the best way to query the dataset. The following works:

/wikipedia/_search?q=mysearch&fields=title,redirect&size=20

but I would like to add more constraints to the search:

disambiguation=false, redirect=false, stub=false, special=false

I'm new to ElasticSearch and the documentation hasn't gotten me far. From what I've read I need a filtered query; is there a way to do that from a GET request? That would make it much easier for my specific use case. If not, how would the POST request look? Thanks in advance.

For reference, the mapping is:

{
  "wikipedia": {
    "page": {
      "properties": {
        "category": {
          "type": "string"
        },
        "disambiguation": {
          "type": "boolean"
        },
        "link": {
          "type": "string"
        },
        "redirect": {
          "type": "boolean"
        },
        "special": {
          "type": "boolean"
        },
        "stub": {
          "type": "boolean"
        },
        "text": {
          "type": "string"
        },
        "title": {
          "type": "string"
        }
      }
    }
  }
}

Upvotes: 0

Views: 181

Answers (1)

moliware
moliware

Reputation: 10278

For adding more constraints you can continue with the lucene syntax and do something like:

/wikipedia/_search?q=mysearch AND disambiguation:false AND redirect:false AND stub:false AND special:false&fields=title,redirect&size=20

For improving the performance you can use filters using the json API, the query will look like:

curl -XGET 'http://localhost:9200/wikipedia/_search?pretty=true' -d '
{
    "from" : 0,
    "size" : 20,
    "query":{
        "filtered" : {
            "query" : {
                "text" : { "title" : "test" }
            },
            "filter" : {
                "and": [
                    {
                        "term" : {
                            "stub" : false
                        }
                    },
                    {
                        "term" : {
                            "disambiguation" : false
                        }
                    },
                    {
                        "term" : {
                            "redirect" : false
                        }
                    },
                    {
                        "term" : {
                            "special" : false
                        }
                    }
                ]
            }
        }
    }
}
' 

Upvotes: 1

Related Questions