0

I'm trying to search Doc model has_and_belongs_to_many projects which belongs to specific project.

models/doc.rb require 'elasticsearch/model'

class Doc < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings index: {
    analysis: {
      tokenizer: {
        ngram_tokenizer: {
          type: "nGram",
          min_gram: "2",
          max_gram: "3",
          token_chars: [
            "letter",
            "digit",
            "punctuation"
          ]
        }
      },
      analyzer: {
        ngram_analyzer: {
          tokenizer: "ngram_tokenizer"
        }
      },
    },
  } do
      mappings do
        indexes :sourcedb, type: 'string', analyzer: 'ngram_analyzer'
        indexes :sourceid, type: 'string', analyzer: 'ngram_analyzer'
        indexes :body, type: 'string', analyzer: 'ngram_analyzer'
        indexes :docs_projects do
          indexes :doc_id
          indexes :project_id
          indexes :projects do
            indexes :id, index: :not_analyzed
          end
        end
      end
  end

  def as_indexed_json(options={})
    as_json(
      only: [:id, :sourcedb, :sourceid, :body],
      include: { projects: {only: :id} }  
    )
  end
end

search method is below

  search_docs = docs.search(
    query: {
      bool:{
        must: [
          {match: {
              'projects.id' => project_id
            }
          }
        ]
      }
    },
    size: 5000,
  ).records.order('sourcedb ASC, sourceid ASC').paginate(page:params[:page], per_page: 10)

This search method finishes without errors but nothing returned.

  Doc Load (4.6ms)  SELECT "docs".* FROM "docs" INNER JOIN "docs_projects" ON "docs"."id" = "docs_projects"."doc_id" WHERE "docs_projects"."project_id" = 56
   (0.4ms)  SELECT COUNT(*) FROM "docs" WHERE 1=0
  Doc Load (0.3ms)  SELECT "docs".* FROM "docs" WHERE 1=0 ORDER BY sourcedb ASC, sourceid ASC LIMIT 10 OFFSET 0
  CACHE (0.0ms)  SELECT COUNT(*) FROM "docs" WHERE 1=0

I've tried to search with where([projects.id IN (?)], project_ids), but it cannot search docs belongs to project with max size.
How to search by match with associations?
Thanks in advance.

Val
  • 207,596
  • 13
  • 358
  • 360
DIGITALSQUAD
  • 3,384
  • 3
  • 22
  • 24

1 Answers1

0

Now, I found the solution. This is how I solved.

doc.rb

  settings index: {
    analysis: {
      tokenizer: {
        ngram_tokenizer: {
          type: "nGram",
          min_gram: "2",
          max_gram: "3",
          token_chars: [
            "letter",
            "digit",
            "punctuation"
          ]
        }
      },
      analyzer: {
        ngram_analyzer: {
          tokenizer: "ngram_tokenizer"
        }
      },
    },
  } do
      mappings do
        indexes :sourcedb, type: 'string', analyzer: 'ngram_analyzer'
        indexes :sourceid, type: 'string', analyzer: 'ngram_analyzer'
        indexes :body, type: 'string', analyzer: 'ngram_analyzer'

        # indexes :docs_projects, type: 'nested' do
        indexes :docs_projects do
          indexes :doc_id
          indexes :project_id
        end

        indexes :projects do
          indexes :id, index: :not_analyzed
        end
      end
  end

search method

  def self.search_docs(attributes = {})
    minimum_should_match = 0
    minimum_should_match += 1 if attributes[:sourcedb].present?
    minimum_should_match += 1 if attributes[:sourceid].present?
    minimum_should_match += 1 if attributes[:body].present?

    if attributes[:project_id].present?
      must_array = [
        {match: {
            'projects.id' => attributes[:project_id]
          }
        }
      ] 
    end

    docs = search(
      query: {
        bool:{
          must: must_array,
          should: [ 
            {match: {
              sourcedb: {
                query: attributes[:sourcedb],
                fuzziness: 0
              }
            }
            },
            {match: {
              sourceid: {
                query: attributes[:sourceid],
                fuzziness: 0
              }
            }
            },
            {match: {
              body: {
                query: attributes[:body],
                fuzziness: 'AUTO'
              }
            }
            },
          ],
          minimum_should_match: minimum_should_match
        }
      },
      size: SEARCH_SIZE,
    )
    return {
      total: docs.results.total,
      docs: docs.records.order('sourcedb ASC, sourceid ASC')
    }
  end
DIGITALSQUAD
  • 3,384
  • 3
  • 22
  • 24