Search query java

i have created a search index named “test_index” for the field/column name “primary skills”. now i need an API for access this search index . so that whn i search the term “java” and i get the result like Java, JAVA, jaVA

Hi @Deepak_Kumar20,

What’s the index definition you’re currently using?

It’ll also be good if you can share what you’ve tried so far including the results you’ve gotten. Additionally, please provide some sample documents based off those example terms you’ve mentioned if you still require assistance on this one.

Regards,
Jason

i have used “ngram” analyzer with minimum gram 3 and max gram 5. and i added the screenshot of the result for one path pin 1 is mongoDB UI and pic 2 is of flask application . i am unable add image as it says new user can only use embedded image so i copy and pasted the result.

{
“PHD_institute”: “”,
“_id”: “653eacd01d5de13ea382a07f”,
“any_entrepreneurship_experience”: “”,
“application_ID”: 215,
“applied_job_ID_list”: “-b3f”,
“blog_or_articles”: “”,
“candidate_age”: 0,
“college_clubs”: “”,
“current_city”: “”,
“current_country”: “”,
“current_country_code”: “”,
“current_employer”: “”,
“current_role”: “SDE”,
“current_salary”: “0”,
“current_state”: “”,
“current_status”: "
“current_zip_code”: “”,
“date_of_birth”: “0000-00-00”,
“department”: “”,
“diploma_institute_college”: “”,
“diploma_qualification”: “”,
“diploma_year_of_completion”: 0,
“discord_profile_link”: “”,
“email_ID”: “”,
“expected_salary”: “0”,
“facebook_profile_link”: “”,
“field_of_PHD_or_research_paper”: “”,
“full_name”: "
“gender”: “”,
“git_account_link”: “”,
“govt_ID”: “”,
“govt_ID_Link”: “”,
“hackathon_date_list”: “”,
“hackathon_technology”: “”,
“hackathon_with”: “”,
“highest_qualification”: “”,
“hobbies”: “”,
“industry”: “”,
“instagram_profile_link”: “”,
“institute_list”: “”,
“is_actively_searching”: 0,
“is_serving_up_notice_period”: 0,
“language”: “”,
“last_working_day”: “1969-12-29”,
“level_of_certification”: “”,
“linkedin_profile_link”: “”,
“location_of_hackathon”: “”,
“marital_status”: “”,
“name_of_hackathon_participated”: “”,
“notice_period”: 0,
“other_skills”: “”,
“past_employer_list”:
“patent”: “”,
“pg_college”: “”,
“pg_education_type”:
“preffered_work_mode”: “”,
“primary_skills”: “["java"]”,
“public_speaking_season”: “”,
“qualification_list”: “”,
“record_updatedts”: “2023-10-29 17:19:02”,
“secondary_skills”: “”,
“topic_of_PHD_or_research_paper”: “”,
“u_id”: “ad24cfce-e906-40c1-92fb-e46ceb9d0160”,
“ug_college”: “”,
“ug_education_type”: “”,
“ug_qualification”: “”,
“ug_year_of_completion”: 0,
“verified_certificate_list”: “”,
“volunteering_fields”: “”,
“volunteering_works”: “”,
“work_mode”: “”,
“work_permit”: “”,
“work_permit_country”: “”,
“work_to_highlight”: “”,
“yoe”: 0
},
{
“PHD_institute”: “”,
“_id”: “653eacd01d5de13ea382a003”,
“any_entrepreneurship_experience”: “”,
“application_ID”: 278,
“applied_job_ID_list”:
“blog_or_articles”: “”,
“candidate_age”: 0,
“college_clubs”: “”,
“current_city”: “”,
“current_country”: “”,
“current_country_code”: “”,
“current_employer”: “”,
“current_role”: “SDE”,
“current_salary”: “0”,
“current_state”: “”,
“current_status”: “”,
“current_zip_code”: “”,
“date_of_birth”: “0000-00-00”,
“department”: “”,
“diploma_institute_college”: “”,
“diploma_qualification”: “”,
“diploma_year_of_completion”: 0,
“discord_profile_link”: “”,
“email_ID”: “”,
“expected_salary”: “0”,
“facebook_profile_link”: “”,
“field_of_PHD_or_research_paper”: “”,
“full_name”: “lehip”,
“gender”: “”,
“git_account_link”: “”,
“govt_ID”: “”,
“govt_ID_Link”: “”,
“hackathon_date_list”: “”,
“hackathon_technology”: “”,
“hackathon_with”: “”,
“highest_qualification”: “”,
“hobbies”: “”,
“industry”: “”,
“instagram_profile_link”: “”,
“institute_list”: “”,
“is_actively_searching”: 0,
“is_serving_up_notice_period”: 0,
“language”: “”,
“last_working_day”: “1969-12-30”,
“level_of_certification”: “”,
“linkedin_profile_link”: “”,
“location_of_hackathon”: “”,
“marital_status”: “”,
“name_of_hackathon_participated”: “”,
“notice_period”: 0,
“other_skills”: “”,
“past_employer_list”:",
“patent”: “”,
“pg_college”: “”,
“pg_education_type”: “”,
“pg_qualification”: “”,
“pg_year_of_completion”: 0,
“preferred_city_location”: “noida”,
“preferred_country”: “”,
“preferred_location_zip_code”: “”,
“preferred_organisation”: “”,
“preferred_state_location”: “”,
“preffered_work_mode”: “”,
“primary_skills”: “["java"]”,
“public_speaking_season”: “”,
“qualification_list”: “”,
“record_updatedts”: “2023-10-29 18:30:06”,
“secondary_skills”: “”,
“topic_of_PHD_or_research_paper”: “”,
“u_id”: “4dbe1767-9b03-4f7d-a4de-a6429f248766”,
“ug_college”: “”,
“ug_education_type”: “”,
“ug_qualification”: “”,
“ug_year_of_completion”: 0,
“verified_certificate_list”: “”,
“volunteering_fields”: “”,
“volunteering_works”: “”,
“work_mode”: “”,
“work_permit”: “”,
“work_permit_country”: “”,
“work_to_highlight”: “”,
“yoe”: 0
},
{
“PHD_institute”: “”,
“_id”: “653eacd01d5de13ea382a071”,
“any_entrepreneurship_experience”: “”,
“application_ID”: 287,
“applied_job_ID_list”: "
“blog_or_articles”: “”,
“candidate_age”: 0,
“college_clubs”: “”,
“current_city”: “Noida”,
“current_country”: “”,
“current_country_code”: “”,
“current_employer”: “”,
“current_role”: “SDE”,
“current_salary”: “0”,
“current_state”: “”,
“current_status”: "
“current_zip_code”: “”,
“date_of_birth”: “0000-00-00”,
“department”: “”,
“diploma_institute_college”: “”,
“diploma_qualification”: “”,
“diploma_year_of_completion”: 0,
“discord_profile_link”: “”,
“email_ID”: “”,
“expected_salary”: “0”,
“facebook_profile_link”: “”,
“field_of_PHD_or_research_paper”: “”,
“full_name”: “jczpqcy6”,
“gender”: “”,
“git_account_link”: “”,
“govt_ID”: “”,
“govt_ID_Link”: “”,
“hackathon_date_list”: “”,
“hackathon_technology”: “”,
“hackathon_with”: “”,
“highest_qualification”: “”,
“hobbies”: “”,
“industry”: “”,
“instagram_profile_link”: “”,
“institute_list”: “”,
“is_actively_searching”: 0,
“is_serving_up_notice_period”: 0,
“language”: “”,
“last_working_day”: “1969-12-31”,
“level_of_certification”: “”,
“linkedin_profile_link”: “”,
“location_of_hackathon”: “”,
“marital_status”: “”,
“name_of_hackathon_participated”: “”,
“notice_period”: 0,
“other_skills”: “”,
“past_employer_list”: "
“patent”: “”,
“pg_college”: “”,
“pg_education_type”: “”,
“pg_qualification”: “”,
“pg_year_of_completion”: 0,
“preferred_city_location”:
“preffered_work_mode”: “”,
“primary_skills”: “["java"]”,
“public_speaking_season”: “”,
“qualification_list”: “”,
“record_updatedts”: “2023-10-29 18:32:45”,
“secondary_skills”: “”,
“topic_of_PHD_or_research_paper”: “”,
“u_id”: “a80913e0-1d22-47b5-a3c5-dac00dff6334”,
“ug_college”: “”,
“work_to_highlight”: “”,
“yoe”: 0
},

here is my python code:- from flask import Flask, request, jsonify
from pymongo import MongoClient
from bson import ObjectId

app = Flask(name)

MongoDB connection setup

client = MongoClient(‘mongodb+srv://admin:password@cluster0.kuetlkb.mongodb.net/user_data’)
collection = client[‘user_data’][‘user_data_table’]

@app.route(‘/search’, methods=[‘GET’])
def search():
# Get the query parameters from the request
query = request.args.get(‘query’)
path = request.args.get(‘path’)

# MongoDB aggregation pipeline to perform text search
result = collection.aggregate([
    {
        '$search': {
            'index': 'test',
            'text': {
                'query': query,
                'path': path
            }
        }
    }
])

# Convert the MongoDB cursor to a list of dictionaries
search_results = list(result)

# Convert ObjectId to string for JSON serialization
for result in search_results:
    result['_id'] = str(result['_id'])

return jsonify(search_results)

if name == ‘main’:
app.run(debug=True) and this the search result i am getting for one path/parameter/column:- {
“PHD_institute”: “”,
“_id”: “653eacd01d5de13ea382a07f”,
“any_entrepreneurship_experience”: “”,
“application_ID”: 215,
“applied_job_ID_list”: “c44824f5-c834-407f-8aa8-e520c12c0abe, 8c6c7e68-16a8-41c9-b135-2c7b847419a4, 8bacb230-3d65-4386-b76c-8f200cc576a2, e07c9236-2468-4fb7-a82d-f622111bb56e, 8296a9d5-3268-40e0-afb5-a7438582602c, aa1af08f-9303-41e2-a299-43695d4b9e45, 2d6c8028-0a12-403a-8e69-b3f”,
“blog_or_articles”: “”,
“candidate_age”: 0,
“college_clubs”: “”,
“current_city”: “Noida”,
“current_country”: “”,
“current_country_code”: “”,
“current_employer”: “”,
“current_role”: “SDE”,
“current_salary”: “0”,
“current_state”: “”,
“current_status”: “APPLIED, APPLIED, Rejected, APPLIED, APPLIED, SCREEN SELECT, INTERVIEW, REJECTED, REJECTED, REJECTED”,
“current_zip_code”: “”,
“date_of_birth”: “0000-00-00”,
“department”: “”,
“diploma_institute_college”: “”,
“diploma_qualification”: “”,
“diploma_year_of_completion”: 0,
“discord_profile_link”: “”,
“email_ID”: “”,
“expected_salary”: “0”,
“facebook_profile_link”: “”,
“field_of_PHD_or_research_paper”: “”,
“full_name”: “Sunil Kr G”,
“gender”: “”,
“git_account_link”: “”,
“govt_ID”: “”,
“govt_ID_Link”: “”,
“hackathon_date_list”: “”,
“hackathon_technology”: “”,
“hackathon_with”: “”,
“highest_qualification”: “”,
“hobbies”: “”,
“industry”: “”,
“instagram_profile_link”: “”,
“institute_list”: “”,
“is_actively_searching”: 0,
“is_serving_up_notice_period”: 0,
“language”: “”,
“last_working_day”: “1969-12-29”,
“level_of_certification”: “”,
“linkedin_profile_link”: “”,
“location_of_hackathon”: “”,
“marital_status”: “”,
“name_of_hackathon_participated”: “”,
“notice_period”: 0,
“other_skills”: “”,
“past_employer_list”: “WUElev8”,
“patent”: “”,
“pg_college”: “”,
“pg_education_type”: “”,
“pg_qualification”: “”,
“pg_year_of_completion”: 0,
“preferred_city_location”: “noida”,
“preferred_country”: “”,
“preferred_location_zip_code”: “”,
“preferred_organisation”: “”,
“preferred_state_location”: “”,
“preffered_work_mode”: “”,
“primary_skills”: “["java"]”,

    "full_name": "lehip80363",
    "gender": "",
    "git_account_link": "",
    "govt_ID": "",
    "govt_ID_Link": "",
    "hackathon_date_list": "",
    "hackathon_technology": "",
    "hackathon_with": "",
    "highest_qualification": "",
    "hobbies": "",
    "industry": "",
    "instagram_profile_link": "",
    "institute_list": "",
    "is_actively_searching": 0,
    "is_serving_up_notice_period": 0,
    "language": "",
    "last_working_day": "1969-12-30",
    "level_of_certification": "",
    "linkedin_profile_link": "",
    "location_of_hackathon": "",
    "marital_status": "",
    "name_of_hackathon_participated": "",
    "notice_period": 0,
    "other_skills": "",
    "past_employer_list": "",
    "patent": "",
    "pg_college": "",
    "pg_education_type": "",
    "pg_qualification": "",
    "pg_year_of_completion": 0,
    "preferred_city_location": "noida",
    "preferred_country": "",
    "preferred_location_zip_code": "",
    "preferred_organisation": "",
    "preferred_state_location": "",
    "preffered_work_mode": "",
    "primary_skills": "[\"java\"]",
    "public_speaking_season": "",
    "qualification_list": "",
    "record_updatedts": "2023-10-29 18:30:06",
    "secondary_skills": "",
    "topic_of_PHD_or_research_paper": "",
    "u_id": "4dbe1767-9b03-4f7d-a4de-a6429f248766",
    "ug_college": "",
    "ug_education_type": "",
    "ug_qualification": "",
    "ug_year_of_completion": 0,
    "verified_certificate_list": "",
    "volunteering_fields": "",
    "volunteering_works": "",
    "work_mode": "",
    "work_permit": "",
    "work_permit_country": "",
    "work_to_highlight": "",
    "yoe": 0
},
{
    "PHD_institute": "",
    "_id": "653eacd01d5de13ea382a071",
    "any_entrepreneurship_experience": "",
    "application_ID": 287,
    "applied_job_ID_list": "5179f1f5-5cd1-4345-92e5-562c92eeefbb, 10b9bf87-292c-441a-92ec-ac90906e4e07",
    "blog_or_articles": "",
    "candidate_age": 0,
    "college_clubs": "",
    "current_city": "",
    "current_country": "",
    "current_country_code": "",
    "current_employer": "",
   
    "govt_ID": "",
    "govt_ID_Link": "",
    "hackathon_date_list": "",
    "hackathon_technology": "",
    "hackathon_with": "",
    "highest_qualification": "",
    "hobbies": "",
    "industry": "",
    "instagram_profile_link": "",
    "institute_list": "",
    "is_actively_searching": 0,
    "is_serving_up_notice_period": 0,
    "language": "",
    "last_working_day": "",
    "level_of_certification": "",
    "linkedin_profile_link": "",
    "location_of_hackathon": "",
    "marital_status": "",
    "name_of_hackathon_participated": "",
    "notice_period": 0,
    "other_skills": "",
    "past_employer_list": "",
    "patent": "",
    "pg_college": "",
    "pg_education_type": "",
    "pg_qualification": "",
    "pg_year_of_completion": 0,
    "preferred_city_location": "",
    "preferred_country": "",
    "preferred_location_zip_code": "",
    "preferred_organisation": "",
    "preferred_state_location": "",
    "preffered_work_mode": "",
    "primary_skills": "[\"java\"]",
    "public_speaking_season": "",
    "qualification_list": "",
    "record_updatedts": "2023-10-29 18:32:45",
    "secondary_skills": "",
    "topic_of_PHD_or_research_paper": "",
    "u_id": "a80913e0-1d22-47b5-a3c5-dac00dff6334",
    "ug_college": "",
    "ug_education_type": "",
    "ug_qualification": "",
    "ug_year_of_completion": 0,
    "verified_certificate_list": "",
    "volunteering_fields": "",
    "volunteering_works": "",
    "work_mode": "",
    "work_permit": "",
    "work_permit_country": "",
    "work_to_highlight": "",
    "yoe": 0
},                                                                                                                                                                                  now i need help: on how can i structure query to search data from two column/path/parameters   for ex:- all user with primary skill java and yoe 5 years

Hi @Deepak_Kumar20,

I can’t see the index definition here but your sample documents provided seem to have the primary_skills field as a string but the string value itself contains square brackets. Is this expected?

You can find the index definition in the Atlas UI in the Atlas Search indexes section viewing / editing the index in the JSON editor.

Additionally, it’s quite difficult to read the information provided. Can you try reformatting the post? Please see Formatting code and log snippets in posts for more details. I noticed there are syntax errors as well (missing commas & quotation marks). This makes it difficult if community users try to assist with the issue when they copy and paste document(s) or code snippets into their test environments.

Regards,
Jason

FWIW, I added the following sample documents (removed other fields for readability):

Atlas atlas-5yfvw3-shard-0 [primary] test> db.collection.find({},{_id:0})
[
  { primary_skills: [ 'java' ] },
  { primary_skills: [ 'jAVA' ] },
  { primary_skills: [ 'JaVa' ] },
  { primary_skills: [ 'javA' ] }
]

Using the default search index definition and search term of "java", all the results get returned:

db.collection.aggregate({
   "$search":{
      "text":{
         "query":"java",
         "path":"primary_skills"
      }
   }
})
[
  {
    _id: ObjectId('657f7b07b4f063d7c3533e81'),
    primary_skills: [ 'java' ]
  },
  {
    _id: ObjectId('657f7b0ab4f063d7c3533e82'),
    primary_skills: [ 'jAVA' ]
  },
  {
    _id: ObjectId('657f7b0fb4f063d7c3533e83'),
    primary_skills: [ 'JaVa' ]
  },
  {
    _id: ObjectId('657f7b16b4f063d7c3533e84'),
    primary_skills: [ 'javA' ]
  }
]

Regards,
Jason

i have to search my all candidates whose primary_skill is “java” and “yoe”(years of experience) is 5 years.
primary skill and yoe are name of column.
name of index is " test"
index type is “search”
index fields are “candidate_age”, "primary_skills " and “yoe”

this is my python application code:-
from flask import Flask, request, jsonify
from pymongo import MongoClient
from bson import ObjectId

app = Flask(name)

client = MongoClient(‘mongodb+srv://admin:password@cluster0.kuetlkb.mongodb.net/user_data’)
collection = client[‘user_data’][‘user_data_table’]

@app.route(‘/search’, methods=[‘GET’])
def search():

Get the query parameters from the request

query = request.args.get(‘query’)
path = request.args.get(‘path’)

MongoDB aggregation pipeline to perform text search

result = collection.aggregate([
{
‘$search’: {
‘index’: ‘test’,
‘text’: {
‘query’: query,
‘path’: path
}
}
}
])

Convert the MongoDB cursor to a list of dictionaries

search_results = list(result)

Convert ObjectId to string for JSON serialization

for result in search_results:
result[‘_id’] = str(result[‘_id’])

return jsonify(search_results)
if name == ‘main’:
app.run(debug=True)

how can structure the query result = collection.aggregate([
{
‘$search’: {
‘index’: ‘test’,
‘text’: {
‘query’: java,
‘path’: primary_skills
}
}
}
])
to search all candidates whose primary_skill is “java” and yoe is 5 years.

Consider using the compound operator. I assume you’ll just need to use text and equals (or range if you want 5 years and greater of experience).

Example documents and $search query using equals:

test> db.collection.find({},{_id:0})
[
  { primary_skills: [ 'java' ], yoe: 3 },
  { primary_skills: [ 'jAVA' ], yoe: 4 },
  { primary_skills: [ 'JaVa' ], yoe: 5 }, /// <--- 5 years of experience
  { primary_skills: [ 'javA' ], yoe: 5 } /// <--- 5 years of experience
]

Query and output:

test> db.collection.aggregate([
  {
    '$search': {
      compound: {
        must: [
          { text: { query: 'java', path: 'primary_skills' } },
          { equals: { path: 'yoe', value: 5 } }
        ]
      }
    }
  }
])
[
  {
    _id: ObjectId('657f7b0fb4f063d7c3533e83'),
    primary_skills: [ 'JaVa' ],
    yoe: 5
  },
  {
    _id: ObjectId('657f7b16b4f063d7c3533e84'),
    primary_skills: [ 'javA' ],
    yoe: 5
  }
]

Regards,
Jason

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.