0

What I'm trying to achieve here is to search for two fields in an index. The structure looks like this in kibana :

POST arempris/emptagnames
{
  "mappings" : {
    "emptags":{
          "properties": {
                "employeeid": {
                  "type":"integer"
                },
                "tagName": {
                  "type": "text",
                  "fielddata": true,
                  "analyzer": "english"
                }
            }    
        }
    }
}

In java, I had written a code to search for the single field named "tagName" and return the searchHits in jsonArrayBuilder format.

Similarly to this, in this case, is it possible to search employeeid first and then return the list of tagNames associated to that employeeid?

In case of RDBMS, my query would look like : SELECT tagNames from filemetadata where employeeid = 124 and tagName like %slip% and this is what I'm looking to implement using QueryBuilder. Is it possible to implement?

The java code to search for a single field in the index what I've written is :

Client esclient = getClient();

        String index = "arempris";
        String type = "tags";

        QueryBuilder query = QueryBuilders.matchPhrasePrefixQuery("tagName", tagName);
        SearchResponse response = esclient.prepareSearch(index).setTypes(type)
                .setQuery(query)
                .execute().actionGet();

        hits = response.getHits().getHits();
        System.out.println(hits.length);
        for (int i = 0; i < hits.length; i++) {
            System.out.println(hits[i].getSourceAsString());
        }

Would greatly appreciate your help and thanks in advance.

1 Answer 1

2

You can use a bool query with must clause to have the AND behaviour. You can use a term query for the exact match and a wildcard query for the like

BoolQueryBuilder query = new BoolQueryBuilder();
query.must(new WildcardQueryBuilder("tagName", "*slip*"));
query.must(new TermQueryBuilder("employeeid", 123));
Sign up to request clarification or add additional context in comments.

7 Comments

ya, forgot to mention, it's showing result for only lowercase entries, for case insensitive, what do we need to add anything?
Yes you have to create a custom analyzer that will use the english analyzer and also lowercase. elastic.co/guide/en/elasticsearch/reference/5.4/…
Sorry I had difficulty understanding it, I tried it, can you provide an example? or any other reference link?
Please open a new topic. There is no use for anyone else if we solve it here.
Yes.Understood and no offence. Actually want to thank you for taking your time to respond to me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.