Skip to content

Instantly share code, notes, and snippets.

@imotov
Last active December 17, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imotov/737176fd5a0f60730db8 to your computer and use it in GitHub Desktop.
Save imotov/737176fd5a0f60730db8 to your computer and use it in GitHub Desktop.
curl -XDELETE localhost:9200/test-idx
curl -XPUT localhost:9200/test-idx -d '{
"mappings": {
"ofertas": {
"properties": {
"idtipooa": {
"type": "nested",
"properties": {
"nombre": {"type": "string", "index": "not_analyzed" }
}
}
}
}
}
}'
echo
curl -XPUT localhost:9200/test-idx/ofertas/3 -d '{
"id": 3,
"nombreoa": "Abogacia",
"titulo": "Abogado",
"idtipooa": {
"nombre": "Carrera Universitaria"
}
}'
echo
curl -XPOST localhost:9200/test-idx/_refresh
echo
echo
echo '== Searching for "Universitaria" - no results =='
curl -XPOST "localhost:9200/test-idx/ofertas/_search?pretty=true" -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "idtipooa",
"query": {
"bool": {
"must": [{
"match": {
"idtipooa.nombre": "Universitaria"
}
}]
}
},
"_cache": true
}
}
}
}
}'
echo
echo
echo '== Searching for "Carrera Universitaria" - one result =='
curl -XPOST "localhost:9200/test-idx/ofertas/_search?pretty=true" -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "idtipooa",
"query": {
"bool": {
"must": [{
"match": {
"idtipooa.nombre": "Carrera Universitaria"
}
}]
}
},
"_cache": true
}
}
}
}
}'
@FUT
Copy link

FUT commented Apr 30, 2014

Hi Igor! Thank you for the gist - it guided me to the proper query structure but finally I was unable to compose it using the gist and elasticsearch (v1.1.0) documentation. Here is what I have:

  def search_with_filters(reservation_ids, query)
    search query: {
      filtered: {
        query: {
          query_string: {
            query: query
          }
        },
        filter: {
          nested: {
            path: 'reservation',
            query: {
              bool: {
                must: {
                  terms: {
                    'reservation.id' => reservation_ids
                  }
                }
              }
            }
          }
        }
      }
    }
  end

And here are the mappings:

=> #<Elasticsearch::Model::Indexing::Mappings:0x0000010fb120a0
 @mapping=
  {:user=>{:type=>"object", :properties=>{:name=>{:type=>"string"}}},
   :reservation=>
    {:nested=>true,
     :type=>"object",
     :properties=>
      {:time_start=>{:type=>"string"},
       :service_location_lane=>
        {:type=>"object", :properties=>{:name=>{:type=>"string"}}}}}},
 @options={},
 @type="employee_reservation">

Could you please point out my mistake? Thank you in advance!

@FUT
Copy link

FUT commented May 12, 2014

Finally I got it. The mapping was wrong. It should be like that:

=> #<Elasticsearch::Model::Indexing::Mappings:0x0000010fb120a0
 @mapping=
  {:user=>{:type=>"object", :properties=>{:name=>{:type=>"string"}}},
   :reservation=>
    {:type=>"nested",
     :properties=>
      {:time_start=>{:type=>"string"},
       :service_location_lane=>
        {:type=>"object", :properties=>{:name=>{:type=>"string"}}}}}},
 @options={},
 @type="employee_reservation">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment