1

I have 2 types Author and Book. I want to search both types and return matches from the fields Author.name and Book.title. How would I complete this query?

GET /bookstore/author,book/_search ...

2 Answers 2

5

You can make a query to the index, not only the type: /yourindex/_search.

Now, you should use a multi match query to control your results better:

{ 
    "multi_match" : { 
        "query": "Will Smith", "fields": [
            "type1.title", "type2.name" 
        ]  
    } 
}

More info: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html

Sign up to request clarification or add additional context in comments.

Comments

1

There are a few approaches based on what you're trying to accomplish.

  1. You could model your data so that you store indexed values for the author in the book type, and possibly a reference to that author.
  2. Otherwise you should be able to query 2 types, however you cannot specify which value to search for in which type. These would technically be 2 separate queries, and you'd have to mix the 2 results yourself if this is what you desire.

It is possible to search both given that book doesn't have an attribute "name", and author doesn't have an attribute "title". If you search for a field that doesn't exist elasticsearch will still try given that the data in the model is schema-less, and a mapping simply defines how to index fields but doesn't necessarily define the structure of the documents.

If you're trying to make a single search for both book and author then I'd recommend doing 2 separate queries, as you'll get 2 separate types of documents which may need to be treated accordingly.

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.