Kiran
Kiran

Reputation: 868

Global Search in Elastic Search

Working on Elasticsearch, my use case is very straight forward. When a user types in a search box I want to search all of my data set irrespective of field or column or any condition (search all data and provide all occurrences of searched word in documents). This might be available in their documentation but I'm not able to understand it. Can somebody explain on this?

Upvotes: 3

Views: 3920

Answers (2)

Vijay
Vijay

Reputation: 5010

Query_string (link) can do this job for u . It support partial search effectively , here is my analysis https://stackoverflow.com/a/43321606/2357869 .
Query_string is more powerful than match , term and wildcard query .

Scenario 1 - Suppose u want to search "Hello" :-

Then go with :-

{
  "query": {
    "query_string": {"query": "*Hello*" }
  }
}

It will search all words like ABCHello , HelloABC , ABCHeloABC

By default it will search hello in all fields (_all)

2) Scenario 2 - Suppose u want to search "Hello" or "World" :-

Then go with :-

{
  "query": {
    "query_string": {"query": "*Hello* *World*" }
  }
}

It will search all words like ABCHello , HelloABC , ABCHelloABC , ABCWorldABC ,ABChello ,ABCworldABC etc.

it will search like Hello OR World , so whichever word having Hello Or world , it wiil give .

By default query_string (link) use default operator OR , u can change that .

Upvotes: 2

Olly Cruickshank
Olly Cruickshank

Reputation: 6180

The easiest way to search across all fields in an index is to use the _all field.

The _all field is a catch-all field which concatenates the values of all of the other fields into one big string, using space as a delimiter, which is then analyzed and indexed, but not stored.

For example:

PUT my_index/user/1 
{
  "first_name":    "John",
  "last_name":     "Smith",
  "date_of_birth": "1970-10-24"
}

GET my_index/_search
{
  "query": {
    "match": {
      "_all": "john smith 1970"
    }
  }
}

Highlighting is supported so matching occurrences can be returned in your search results.

Drawbacks

There are two main drawbacks to this approach:

  1. Additional disk space and memory are needed to store the _all field
  2. You lose flexibility in how the data and search terms are analysed

A better approach is to disable the _all field and instead list out the fields you are interested in:

GET /_search
{
    "query": {
        "query_string" : {
            "query" : "this AND that OR thus",
            "fields":[
                "name",
                "addressline1",
                "dob",
                "telephone",
                "country",
                "zipcode"
             ]
        }
    }
}

Upvotes: 5

Related Questions