1

I have indexed data in elasticsearch . Index name is "demo" . I have two types (mappings) for "demo" , one is "user" and other is "blog". "user" type have fields - name , city , country other fields and blog have - "title" , description" , "author_name" etc. Now I want to search across "demo". If I want to search "java" then it will bring all the documents which have "java" in any fields of any type , either "user" or "blog".

1
  • What solution have you tried? Commented Mar 3, 2015 at 14:11

1 Answer 1

6

You can use the "_all" field for that index. By default each of your fields will be included in the "_all" field for each type. Then you can just run a match query against the "_all" field. Also, when searching the index, just don't specify a type and all types will be searched.

Here is an example:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "user": {
         "properties": {
             "name" : { "type": "string" },
             "city" : { "type": "string" },
             "country" : { "type": "string" }
         }
      },
      "blog": {
         "properties": {
             "title" : { "type": "string" },
             "description" : { "type": "string" },
             "author_name" : { "type": "string" }
         }
      }
   }
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"user"}}
{"name":"Bob","city":"New York","country":"USA"}
{"index":{"_index":"test_index","_type":"user"}}
{"name":"John","city":"Jakarta","country":"Java/Indonesia"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Python/ES","description":"using Python with Elasticsearch","author_name":"John"}
{"index":{"_index":"test_index","_type":"blog"}}
{"title":"Java/ES","description":"using Java with Elasticsearch","author_name":"Bob"}

POST /test_index/_search
{
    "query": {
        "match": {
           "_all": "Java"
        }
    }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.68289655,
      "hits": [
         {
            "_index": "test_index",
            "_type": "blog",
            "_id": "hNJ-AOG2SbS0nw4IPBuXGQ",
            "_score": 0.68289655,
            "_source": {
               "title": "Java/ES",
               "description": "using Java with Elasticsearch",
               "author_name": "Bob"
            }
         },
         {
            "_index": "test_index",
            "_type": "user",
            "_id": "VqfowNx8TTG69buY9Vd_MQ",
            "_score": 0.643841,
            "_source": {
               "name": "John",
               "city": "Jakarta",
               "country": "Java/Indonesia"
            }
         }
      ]
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.