Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search within single document using Elasticsearch

If I want to search an index I can use:

$curl -XGET 'X/index1/_search?q=title:ES'

If I want to search a document type I can use:

$curl -XGET 'X/index1/docType1/_search?q=title:ES'

But if I want to search a specific document, this doesn't work:

$curl -XGET 'X/index1/docType1/documentID/_search?q=title:ES'

Is there a simple work around for this so that I can search within a single document as opposed to an entire index or an entire document type? To explain why I need this, I have to do some resource intensive queries to find what I'm looking for. Once I find the documents I need, I don't actually need the whole document, just the highlighted portion that matches the query. But I don't want to store all the highlighted hits in memory because I might not need them for a few hours and at times they could take up a lot of space (I would also prefer not to write them to disk). I'd rather store a list of document ids so that when I need the highlighted portion of a document I can just run the highlighted query on a specific document and get back the highlighted portion. Thanks in advance for your help!

like image 673
Cam S Avatar asked Jan 06 '15 02:01

Cam S


People also ask

How do I search a particular field in Elasticsearch?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.

How do I search multiple fields in Elasticsearch?

One of the most common queries in elasticsearch is the match query, which works on a single field. And there's another query with the very same options that works also on multiple fields, called multi_match. These queries support text analysis and work really well.

What should you use to fetch a document in Elasticsearch?

Descriptionedit. You use GET to retrieve a document and its source or stored fields from a particular index. Use HEAD to verify that a document exists. You can use the _source resource retrieve just the document source or verify that it exists.

How do I get Elasticsearch data?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .


1 Answers

You can index the document's id as a field, then when you query, include the unique document id as a term to narrow the results just to that single document.

'$curl -XPOST 'X/index1/docType1/_search' -d '{
    "query": {
        "bool": {
              "must":[
                  {"match":{"doc":"223"}},
                  {"match":{"title":"highlight me please"}}
               ]
        }
   }
}'
like image 135
sksamuel Avatar answered Nov 01 '22 12:11

sksamuel