Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:19:31+00:00 2026-05-27T14:19:31+00:00

I like to use Hibernate Search for implementing an sophisticated autosuggestion feature across multiple

  • 0

I like to use Hibernate Search for implementing an sophisticated autosuggestion feature across multiple input fields on a web page.
Each input field is for its own entity, let’s say Country and City. There is a many-to-one relationship between both entities
(countries contain cities).

The autosuggestion should work such that when typing e.g. a country name prefix and the city field is already filled,
you get only suggestions for countries that have such a city (and vice versa).

The server side autosuggestion service should return list of projections
(entityId, entityName) which are rendered into the input field (dropdown, whatever).

According to the schema and after having read the manual I tried the following index schema:

SearchMapping mapping = new SearchMapping();

    mapping.analyzerDef(...
    .entity(City.class).indexed().indexName("MyIndex")
        .property("cityId", ElementType.FIELD)
            .documentId()
            .name("id")
    .property("name", ElementType.FIELD)
            .documentId()
            .name("id")
        .property("country", ElementType.METHOD)
            .indexEmbedded()
    .entity(Country.class).indexed()
        .property("id", ElementType.FIELD)
            .documentId()
            .name("id")
        .property("name", ElementType.METHOD)
            .field()
            .name("name")

This mapping defines City to be the main entity, right?
I have indexed all cities and am able to query for them (also by combining both fields). However, I only get matches when querying for cities.
i.e. when querying like
fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(City.class).get();

This is not useful for the country field becuse when I type in “Spain”, I get a single row for each city of Spain. (Spain, Spain, Spain, Spain ,…. ;-))

The question is: How is it possible to search for country entities? Changing the index structure? The indexing procedure? Or how to query?

The only way I found was to setup a Facet for country, and you the different possible facets as autosuggestion. However, this is also not perfect
since it is not possible to sort facets alphabetically.

Of course, in this example, I could switch both entities in the mapping, but suppose scenarios with more complex entity graphs.

UPDATE: adding queries requested in comment

For building queries, I employ the QueryBuilder. The following produces a result set like in the Spain example:

fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(City.class).get();

with query:

country.name:Spain

If I try to use a query builder for countries

fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Country.class).get();

and query:

name:Spain

I get no results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:19:31+00:00Added an answer on May 27, 2026 at 2:19 pm

    You are not showing your actual query. You don’t have to use the query DSL, but you can also write native Lucene queries. In both cases (DSL or native Lucene) you can combine queries via boolean logic. Embedded entities follow the java bean notation. The country name would for example in a city query be reached as country.name. Again, without your actual query it is hard to give any more specific feedback.

    Last, but not least, facets can also be sorted alphabetically. Check FacetSortOrder.COUNT_DESC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use hibernate for persisting objects through web services and am
I'd like to use org.hibernate.tool.hbm2ddl.SchemaExport from hibernate-core 4.1.4 to generate/export a Sybase ASE 15.5
I would like to use Date() function in hibernate criteria, since one field is
I use Netbeans IDE. My hibernate.cfg.xml looks like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE hibernate-configuration
I would like to use hibernate-3.5-1.Final along with this plugin, what should be my
I'd like to use Hibernate's Criteria API for precisely what everybody says is probably
I would like to use hibernate to contain a hierarchy of objects, however the
I would like to use Lucene to provide full text search capability on some
Is it possible to use hibernate search/lucene to index some entity based on values
Starting a new project I'd like to use Hibernate annotations with MySQL instead of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.