solrsolrj

How to create Solr Query with SolrJClient for the below JSON?


Actuall I am able to hit the local solr via postman , but unable to do same with Solr J Client for the below query . Can some help Howshould I create this query:

{
    "query": {
        "bool": {
            "should": [
                {
                    "edismax": {
                        "query": "\"market value\"",
                        "qf": "title^10",
                        "tie": "0.1",
                        "mm": "100%"
                    }
                },
                {
                    "edismax": {
                        "query": "market+value",
                        "qf": "title^2",
                        "tie": "0.1",
                        "mm": "100%"
                    }
                },
                {
                    "edismax": {
                        "query": "(market* AND value*)",
                        "qf": "title^5",
                        "tie": "0.1",
                        "mm": "100%"
                    }
                },
                {
                    "edismax": {
                        "query": "(market~ AND value~)",
                        "qf": "title^1",
                        "tie": "0.1",
                        "mm": "100%"
                    }
                },
                {
                    "edismax": {
                        "query": "{!knn f=title_vector topK=100}[-0.019111417, -0.025036218, 0.0041847187, -0.013134183]}]}

I have tried below way : query {!bool should=$ref1 should=$ref2 should=$ref3}&ref1=title:"Market Value"&ref2=title:Market+Value&ref3=(title:Market* AND title:Value*)


Solution

  • Solr does provide option doing query in json format and also there solrJ query construction samples.

    Click here for solr official doc page.

    For eg:

    Curl Json query:

    curl -X POST http://localhost:8983/solr/techproducts/query -d '
    {
        "query": {
            "bool": {
                "must": [
                    {"lucene": {"df": "name", query: "iPod"}}
                ],
                "must_not": [
                    {"frange": {"l": "0", "u": "5", "query": "popularity"}}
                ]
            }
        }
    }'
    
    

    SolrJ

    final Map<String, Object> queryTopLevel = new HashMap<>();
    final Map<String, Object> boolProperties = new HashMap<>();
    final List<Object> mustClauses = new ArrayList<>();
    final List<Object> mustNotClauses = new ArrayList<>();
    final Map<String, Object> frangeTopLevel = new HashMap<>();
    final Map<String, Object> frangeProperties = new HashMap<>();
    
    queryTopLevel.put("bool", boolProperties);
    boolProperties.put("must", mustClauses);
    mustClauses.add("name:iPod");
    
    boolProperties.put("must_not", mustNotClauses);
    frangeTopLevel.put("frange", frangeProperties);
    frangeProperties.put("l", 0);
    frangeProperties.put("u", 5);
    frangeProperties.put("query", "popularity");
    mustNotClauses.add(frangeTopLevel);
    
    final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
    final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
    
    final Map<String, Object> queryTopLevel = new HashMap<>();
    final Map<String, Object> boolProperties = new HashMap<>();
    final List<Object> mustClauses = new ArrayList<>();
    final List<Object> mustNotClauses = new ArrayList<>();
    final Map<String, Object> frangeTopLevel = new HashMap<>();
    final Map<String, Object> frangeProperties = new HashMap<>();
    
    queryTopLevel.put("bool", boolProperties);
    boolProperties.put("must", mustClauses);
    mustClauses.add("name:iPod");