Val
Val

Reputation: 41

How can I filter results by custom field in Solr query?

I need some custom field filter for my Solr data like

   {
    "id":"1",
    "name":"Test title",
    "language":"en"
    },
  {
    "id":"2",
    "name":"Test title",
    "language":"fr"
    "parent": "1"
    }

I need to get just first item by query

/select?q=name:test

So I need to filter results by parent field in such way, that one of the items will be present in the result. Thanks for any ideas.

Upvotes: 0

Views: 417

Answers (1)

Lucas Diehl
Lucas Diehl

Reputation: 47

When I need to do querys in Solr I used the SearchQuery() and inside them I set filterQueries. There was possible set filters for my search.

final String FIELD_NAME = "name_text_mv"; // name of my field in Solr
SearchQuery searchQuery = init(facetSearchConfig); // init configs
searchQuery.setFreeTextQueryBuilder(text); // set the text of my search
setFiltersFreeTextSearch(searchQuery.getFilterQueries(), text, FIELD_NAME);

The function to make the magic (add in my search my filters):

private void setFiltersFreeTextSearch(List<QueryField> filters, String text, String... fields) {
    text = StringUtils.stripAccents(text).toLowerCase();
    String textCapitalized = capitalizeEachWolrd(text.toLowerCase());
    for (String field : fields) {
        QueryField queryField = new QueryField(field, SearchQuery.Operator.OR, SearchQuery.QueryOperator.CONTAINS,
                text, text.toUpperCase(), textCapitalized);
        filters.add(queryField);
    }
}

How you can see, in this QueryField you can add the 'wheres' of you search in Solr. I was using CONTAINS and that is my 'LIKE' and 'OR' for find any item.

So basicly you can use QueryField() to add filters for you specifically field.

Well, this was my solution for my case, anyway, this is just an idea. :)

(For the projet is used Java)

Upvotes: 1

Related Questions