Package org.elasticsearch.index.query

Examples of org.elasticsearch.index.query.BoolQueryBuilder


        WrapperQueryBuilder wrapper = new WrapperQueryBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");
        SearchResponse searchResponse = client.prepareSearch().setQuery(wrapper).execute().actionGet();
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));

        BoolQueryBuilder bool = new BoolQueryBuilder();
        bool.must(wrapper);
        bool.must(new TermQueryBuilder("field2", "value2_1"));

        searchResponse = client.prepareSearch().setQuery(wrapper).execute().actionGet();
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));

    }
View Full Code Here


            @Override public void onResponse(GetResponse getResponse) {
                if (!getResponse.exists()) {
                    listener.onFailure(new ElasticSearchException("document missing"));
                    return;
                }
                final BoolQueryBuilder boolBuilder = boolQuery();
                try {
                    DocumentMapper docMapper = indicesService.indexServiceSafe(concreteIndex).mapperService().documentMapper(request.type());
                    final Set<String> fields = newHashSet();
                    if (request.fields() != null) {
                        for (String field : request.fields()) {
                            FieldMappers fieldMappers = docMapper.mappers().smartName(field);
                            if (fieldMappers != null) {
                                fields.add(fieldMappers.mapper().names().indexName());
                            } else {
                                fields.add(field);
                            }
                        }
                    }

                    if (!fields.isEmpty()) {
                        // if fields are not empty, see if we got them in the response
                        for (Iterator<String> it = fields.iterator(); it.hasNext(); ) {
                            String field = it.next();
                            GetField getField = getResponse.field(field);
                            if (getField != null) {
                                for (Object value : getField.values()) {
                                    addMoreLikeThis(request, boolBuilder, getField.name(), value.toString());
                                }
                                it.remove();
                            }
                        }
                        if (!fields.isEmpty()) {
                            // if we don't get all the fields in the get response, see if we can parse the source
                            parseSource(getResponse, boolBuilder, docMapper, fields, request);
                        }
                    } else {
                        // we did not ask for any fields, try and get it from the source
                        parseSource(getResponse, boolBuilder, docMapper, fields, request);
                    }

                    if (!boolBuilder.hasClauses()) {
                        // no field added, fail
                        listener.onFailure(new ElasticSearchException("No fields found to fetch the 'likeText' from"));
                        return;
                    }

                    // exclude myself
                    Term uidTerm = docMapper.uidMapper().term(request.type(), request.id());
                    boolBuilder.mustNot(termQuery(uidTerm.field(), uidTerm.text()));
                } catch (Exception e) {
                    listener.onFailure(e);
                    return;
                }
View Full Code Here

        sb.append("/document_index");
       
        ElasticSearchManager esm = ElasticSearchManager.getIndex(sb.toString());
               
        SearchRequestBuilder searchOptions = esm.getSearchOptions();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.rangeQuery(DocumentPojo.created_).from(cleanseStartTime));
        boolQuery.must(QueryBuilders.termQuery(DocumentPojo.sourceKey_, sp.getKey() ));
        searchOptions.setSize(200); // (note this is multiplied by the number of primary shards)
        searchOptions.setSearchType(SearchType.SCAN);
        searchOptions.setScroll("10m");
        SearchResponse rsp = esm.doQuery(boolQuery, searchOptions);
        String scrollId = rsp.getScrollId();
        int nSkip = 0;
       
        for (;;) // Until no more hits
        {
          rsp = esm.doScrollingQuery(scrollId, "10m");
          SearchHit[] docs = rsp.getHits().getHits();
          scrollId = rsp.getScrollId();
         
          if ((null == docs) || (0 == docs.length)) {
            break;
          }         
          if (docs.length > 100) { // just display large checks)
            logger.info("Checking ES docs for large source=" + sp.getKey() + " source: " + source_index + "/" + source_count + " from " + nSkip + " to " + (nSkip+docs.length) );
          }
         
          //Check all solr docs against mongodb
         
          for (SearchHit hit: docs)
          {
            String idStr = hit.getId();
            boolean found = true; //(fail closed!)
            if (null == dbCache) {
              //OBSOLETED, USE DBCACHE INSTEAD (WHERE AVAILABLE):
              ObjectId id = new ObjectId(idStr);
              BasicDBObject query = new BasicDBObject(DocumentPojo._id_, id);
              query.put(DocumentPojo.sourceKey_, sp.getKey()); // (ensures uses only the right shard)
              DBObject dbo = documentDb.findOne(query, queryFields);
              found = (dbo != null);
            }//TESTED
            else {
              found = dbCache.contains(idStr);
            }//TESTED
            if (!found)
            {       
              ObjectId id = new ObjectId(idStr);
              DocumentPojo doc = new DocumentPojo();
              doc.setId(id);
              doc.setIndex(hit.getIndex() + "/document_index");
              docs_to_remove.add(doc);
              logger.info("db sync removing doc: " + id + "/" + hit.getIndex() + "/" + source_index + " not found in mongo");
              fixcount++;
            } // end if not found
          } // end loop over docs to check
         
          nSkip += docs.length;
        }// until no more hits
        if (!docs_to_remove.isEmpty()) {
          storeManager.removeFromSearch(docs_to_remove);
          docs_to_remove.clear();
        }
       
        //CHECK OLD FEEDS 10 at atime
        int iteration = 1;
        boolean removedAll = true;
        while (removedAll )
        {
          int rows = iteration*iteration*10;//exponential scaling 10x^2
          iteration++;
          int oldfixes = 0;
         
          //get old docs from es
          SearchRequestBuilder searchOptionsOLD = esm.getSearchOptions();
          BoolQueryBuilder boolQueryOLD = QueryBuilders.boolQuery();
          boolQueryOLD.must(QueryBuilders.rangeQuery(DocumentPojo.created_).from(cleanseStartTime));
          boolQueryOLD.must(QueryBuilders.termQuery(DocumentPojo.sourceKey_, sp.getKey()));
          searchOptionsOLD.addSort(DocumentPojo.created_, SortOrder.ASC);
          searchOptionsOLD.setSize(rows);
          SearchResponse rspOLD = esm.doQuery(boolQueryOLD, searchOptionsOLD);
          SearchHit[] docsOLD = rspOLD.getHits().getHits();
         
View Full Code Here

        }
      }//TESTED                   

      ElasticSearchManager esm = ElasticSearchManager.getIndex(assocIndex_);
      SearchRequestBuilder searchOptions = esm.getSearchOptions();
      BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
      boolean bExtraQueryTerms = false;
      String term = "";
      if ( !ent1.equals("null") )
      {
        if ( field.equals(AssociationFeaturePojo.entity1_) )
          term = ent1;
        else {
          bExtraQueryTerms = true;
          EntityFeaturePojo alias = null;
          if (null != aliasTable) {
            alias = aliasTable.getAliasMaster(ent1);
          }
          if (null != alias) { // Found!
            boolQuery.must(QueryBuilders.termsQuery(AssociationFeaturePojo.entity1_index_, alias.getAlias().toArray()));
          }
          else {
            boolQuery.must(QueryBuilders.termQuery(AssociationFeaturePojo.entity1_index_, ent1));
          }//TESTED
        }
      }
      if ( !verb.equals("null") )
      {
        if ( field.equals(AssociationFeaturePojo.verb_) )
          term = verb;
        else
        {
          bExtraQueryTerms = true;
          boolQuery.must(QueryBuilders.queryString(new StringBuffer("+").append(verb.replaceAll("\\s+", " +")).toString()).
              defaultField(AssociationFeaturePojo.verb_));
        }
      }
      if ( !ent2.equals("null") )
      {
        if ( field.equals(AssociationFeaturePojo.entity2_) )
          term = ent2;
        else {
          bExtraQueryTerms = true;
          EntityFeaturePojo alias = null;
          if (null != aliasTable) {
            alias = aliasTable.getAliasMaster(ent2);
          }
          if (null != alias) { // Found!
            boolQuery.must(QueryBuilders.termsQuery(AssociationFeaturePojo.entity2_index_, alias.getAlias().toArray()));
          }
          else {
            boolQuery.must(QueryBuilders.termQuery(AssociationFeaturePojo.entity2_index_, ent2));
          }
        }//TESTED (cut and paste from entity1)
     

      String escapedterm = null;
      StandardTokenizer st = new StandardTokenizer(Version.LUCENE_30, new StringReader(ContentUtils.stripDiacritics(term)));
      CharTermAttribute termAtt = st.addAttribute(CharTermAttribute.class);
      StringBuffer sb = new StringBuffer();
      try {
        try {
          st.reset();
          while (st.incrementToken()) {
            if (sb.length() > 0) {
              sb.append(" +");
            }
            else {
              sb.append('+');           
            }
            sb.append(luceneEncodeTerm(termAtt.toString()));
          }
        }
        finally {
          st.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }     
      if (!term.endsWith(" ") || (0 == sb.length())) { // Could be in the middle of typing, stick a * on the end
        sb.append('*');
      }//TESTED     

      escapedterm = sb.toString();
     
      // Also create an internal Lucene index for aliases, in case any of them do not have actual entities representing them
      List<EntityFeaturePojo> extraEntries = null;
      BoolQueryBuilder extraQueryTerms = null;
      if (field.startsWith("entity")) {
        String indexField = field.startsWith("entity1") ? "entity1_index" : "entity2_index";
        if (null != aliasTable) {
          extraEntries = checkAliasMasters(aliasTable, escapedterm);
        }
        if (null != extraEntries) {
          extraQueryTerms = QueryBuilders.boolQuery();
          int nExtraTerms = 0;
          Iterator<EntityFeaturePojo> aliasIt = extraEntries.iterator();
          while (aliasIt.hasNext()) {
            EntityFeaturePojo alias = aliasIt.next();           
            nExtraTerms += alias.getAlias().size();
           
            if (!bExtraQueryTerms && (nExtraTerms > 20)) { // If not filtering on event type we'll be more aggressive
              break;
            }//TESTED
            if (bExtraQueryTerms && (nExtraTerms > 60)) { // If the number of terms gets too large bail anyway
              break;
            }//TESTED
           
            extraQueryTerms.should(QueryBuilders.termsQuery(indexField, alias.getAlias().toArray()));
            aliasIt.remove();
           
          }//end loop over entities
        }//if found new aliases
       
      }//(if this is an entity lookup) TESTED - including breaking out because of # of terms
     
      // (end initial alias handling)
     
      if (null == extraQueryTerms) {
        boolQuery.must(QueryBuilders.queryString(escapedterm).defaultField(field));
      }
      else {//(in this case combine the escaped term with the aliases
        extraQueryTerms.should(QueryBuilders.queryString(escapedterm).defaultField(field));
        boolQuery.must(extraQueryTerms);
      }//TESTED
      boolQuery.must(QueryBuilders.termsQuery(AssociationFeaturePojo.communityId_, communityIdStrs));

      searchOptions.addSort(AssociationFeaturePojo.doccount_, SortOrder.DESC);
View Full Code Here

                      FilterBuilders.termFilter(EntityPojo.index_, entIndex)));
          }//TESTED
          else {
            QueryFilterBuilder qfb = null;
            if ((null != alias.getSemanticLinks()) && !alias.getSemanticLinks().isEmpty())  {
              BoolQueryBuilder qb = QueryBuilders.boolQuery();
              for (String textAlias: alias.getSemanticLinks()) {
                qb = qb.should(CrossVersionQueryBuilders.matchPhraseQuery(DocumentPojo.fullText_, textAlias));
              }
              qfb = FilterBuilders.queryFilter(qb);             
            }//TESTED
            if (!alias.getAlias().isEmpty()) {
              NestedFilterBuilder nfb = FilterBuilders.nestedFilter(DocumentPojo.entities_,
View Full Code Here

         
          queryElements[i] = this.parseQueryTerm(query.qt.get(i), (sQueryElements[i] = new StringBuffer()));
         
          // Extra full text terms generated by aliasing:
          if (null != _extraFullTextTerms) {
            BoolQueryBuilder extraTerms = QueryBuilders.boolQuery().should(queryElements[i]);
            StringBuffer discard = new StringBuffer(); // (we already have added the info the query elements)
            for (AdvancedQueryPojo.QueryTermPojo qtExtra: _extraFullTextTerms) {
              extraTerms = extraTerms.should(this.parseQueryTerm(qtExtra, discard));
            }
            queryElements[i] = extraTerms;
            _extraFullTextTerms = null; // (reset ready for next term...)
          }//TESTED       
         
View Full Code Here

  // (Not needed any more, but kept here for illustrative purposes)
  //private static Pattern _luceneExactPattern = Pattern.compile("([\"+~*?:|&(){}\\[\\]\\^\\!\\-\\\\ ])"); 
  private BaseQueryBuilder parseQueryTerm(AdvancedQueryPojo.QueryTermPojo qt, StringBuffer sQueryTerm) {
   
    BaseQueryBuilder term = null;
    BoolQueryBuilder boolTerm = null;
   
    sQueryTerm.setLength(0);
    sQueryTerm.append('(');

  // 1.1] Free text (Lucene) 
   
    boolean nonEmpty = false;
   
    if (null != qt.ftext) { // NOTE term building code below depends on this being 1st clause
      nonEmpty = true;
      if (qt.ftext.startsWith("$cache")) { // currently not supported
        throw new RuntimeException("Don't currently support nested cached queries - coming soon.");
      }
     
      sQueryTerm.append('(');
      if (null != qt.metadataField) {
        sQueryTerm.append(qt.metadataField).append(':');       
      }
      sQueryTerm.append(qt.ftext);     
      sQueryTerm.append(')');
      if (null != qt.metadataField) { // Metadata only
        term = QueryBuilders.queryString(qt.ftext).field(qt.metadataField);
      }
      else {
        term = QueryBuilders.queryString(qt.ftext).field("_all").field(DocumentPojo.fullText_);       
      }
      if (null == _scoringParams.adjustAggregateSig) { // auto-decide .. if ftext is set and is non-trivial
        if (qt.ftext.contains(" ")) {
          _scoringParams.adjustAggregateSig = true;
        }
      }
    }//TESTED (logic0)
   
  // 1.2] Exact text 
   
    if (null != qt.etext) { // NOTE term building code below depends on this being 2nd clause
      nonEmpty = true;
      BaseQueryBuilder termQ = null;
      if (sQueryTerm.length() > 1) {
        sQueryTerm.append(" AND ");
      }
      if (qt.etext.equals("*")) { // Special case
        termQ = QueryBuilders.matchAllQuery();
      }
      else { // Normal query
        if (null != qt.metadataField) { // Metadata only
          termQ = CrossVersionQueryBuilders.matchPhraseQuery(qt.metadataField, qt.etext);
        }
        else { // Normal query
          termQ = QueryBuilders.boolQuery().
            should(CrossVersionQueryBuilders.matchPhraseQuery("_all", qt.etext)).
            should(CrossVersionQueryBuilders.matchPhraseQuery(DocumentPojo.fullText_, qt.etext));
        }
      }
      sQueryTerm.append('(');
      if (null != qt.metadataField) {
        sQueryTerm.append(qt.metadataField).append(':');       
      }
      sQueryTerm.append('"');
      sQueryTerm.append(qt.etext);     
      sQueryTerm.append("\")");
      if (null == term) {
        term = termQ;
      }
      else {
        term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
      }
    }//TESTED (logic1)
   
    // Here's where it starts getting interesting:
 
  // 1.3] Entity  
   
    if ((null != qt.entity) || (null != qt.entityValue) || ((null == qt.assoc) && (null != qt.sentiment))) { // (if no association specified then sentiment applies to entities)
      nonEmpty = true;
      if (sQueryTerm.length() > 1) {
        sQueryTerm.append(" AND ");
      }
      sQueryTerm.append('(');
     
      BaseQueryBuilder termQ = QueryBuilders.nestedQuery(DocumentPojo.entities_, this.parseEntityTerm(qt, sQueryTerm, EntityPojo.docQuery_index_)).scoreMode("max").boost((float)1.0);
     
      if (null == term) {
        term = termQ;
      }
      else if (null == boolTerm) {
        term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
      }
      else {
        term = (boolTerm = boolTerm.must(termQ));
      }
      sQueryTerm.append(')');
     
    }//TESTED: logic2* TOTEST: alias expansion code (logic3)
       
  // 1.4] Dates
   
    if (null != qt.time) {
      nonEmpty = true;
      if (sQueryTerm.length() > 1) {
        sQueryTerm.append(" AND ");
      }
      sQueryTerm.append('(');     
     
      BaseQueryBuilder termQ = this.parseDateTerm(qt.time, sQueryTerm, (null != qt.entityOpt) && qt.entityOpt.lockDate);
     
      if (null == term) {
        term = termQ;
      }
      else if (null == boolTerm) {
        term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
      }
      else {
        term = (boolTerm = boolTerm.must(termQ));
      }
      sQueryTerm.append(')');
     
    }//TESTED (logic5-10)

  // 1.5] Geo 
   
    if (null != qt.geo)
    {
      nonEmpty = true;
      if (sQueryTerm.length() > 1)
      {
        sQueryTerm.append(" AND ");
      }
      sQueryTerm.append('(');
     
      BaseQueryBuilder termQ = this.parseGeoTerm(qt.geo, sQueryTerm, GeoParseField.ALL);
      if (null != termQ)
      {
        if (null == term)
        {
          term = termQ;
        }
        else if (null == boolTerm)
        {
          term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
        }
        else
        {
          term = (boolTerm = boolTerm.must(termQ));
        }       
      }
     
      sQueryTerm.append(')');         
    } // (end geo)
   
    if (null == qt.assoc) qt.assoc = qt.event;
      //(continue to support the old "event" name for another release)
    if (null != qt.assoc) {
      nonEmpty = true;
      if (sQueryTerm.length() > 1) {
        sQueryTerm.append(" AND ");
      }
      sQueryTerm.append('(');
     
      BaseQueryBuilder termQ = QueryBuilders.nestedQuery(DocumentPojo.associations_, this.parseAssociationTerm(qt.assoc, qt.sentiment, sQueryTerm));
      if (null != termQ) {
        if (null == term) {
          term = termQ;
        }
        else if (null == boolTerm) {
          term = (boolTerm = QueryBuilders.boolQuery().must(term).must(termQ));
        }
        else {
          term = (boolTerm = boolTerm.must(termQ));
        }       
      }
     
      sQueryTerm.append(')');               
    } // (end event)
View Full Code Here

     
      sQueryTerm.append(sFieldName).append(":\"").append(qt.entity).append('"');
     
      // Just leave this fixed for entity expansion since we don't really support events anyway
      // we'll have to sort this out later
      BoolQueryBuilder termBoolQ = null;
     
      if ((null != qt.entityOpt) && qt.entityOpt.expandAlias) {
        // Alias expansion code
        // Easy bit:
         termBoolQ = QueryBuilders.boolQuery().should(QueryBuilders.termQuery(sFieldName, qt.entity));
        // Interesting bit:
        if (null != _tmpAliasMap) {
          String[] terms = _tmpAliasMap.get(qt.entity).toArray(new String[0]);
          if ((null != terms) && (terms.length > 0)) {
            termQ = termBoolQ.should(QueryBuilders.termsQuery(EntityPojo.docQuery_actual_name_, terms));
            sQueryTerm.append(" OR actual_name:$aliases");
          }
        }
      }//TESTED logic3a,b,f - actual_name expansion
     
      boolean isKeyword = false;
      if (null != qt.entity) {
        isKeyword = qt.entity.endsWith("/keyword");
      }
      else if (null != qt.entityType) {
        isKeyword = qt.entityType.equalsIgnoreCase("Keyword");
      }
     
      if (((null != qt.entityOpt) && qt.entityOpt.rawText) || isKeyword)
      {
        if (null == this._extraFullTextTerms) {
          _extraFullTextTerms = new LinkedList<AdvancedQueryPojo.QueryTermPojo>();
        }
        String dName = qt.entityValue;
        if (null == dName) { // Derive disambiguated name if not directly specified
          int nIndex2 = qt.entity.lastIndexOf('/');
          dName = qt.entity.substring(0, nIndex2);
        }//TESTED

        if (EntityPojo.docQuery_index_ == sFieldName) { // (note: can use pointers here)
          AdvancedQueryPojo.QueryTermPojo qtExtra = new AdvancedQueryPojo.QueryTermPojo();
          qtExtra.etext = dName;
          _extraFullTextTerms.add(qtExtra);
          sQueryTerm.append(" OR ((\"").append(dName).append('"').append("))");
        }
        else if (AssociationPojo.docQuery_geo_index_ != sFieldName) { // (geo has no non-indexed form)
          String nonIndexField = (AssociationPojo.docQuery_entity1_index_ == sFieldName) ? AssociationPojo.docQuery_entity1_ : AssociationPojo.docQuery_entity2_;
          if (null == termBoolQ) {
            termBoolQ = QueryBuilders.boolQuery();
          }
          termQ = termBoolQ = termBoolQ.should(CrossVersionQueryBuilders.matchPhraseQuery(nonIndexField, dName));
          sQueryTerm.append(" OR ").append(nonIndexField).append(":\"").append(dName).append('"');         
        }//TESTED
      }
      //TESTED (entity+association) - entity options, add dname as exact text query
     
      if (null != _aliasLookup) {
        EntityFeaturePojo masterAlias = _aliasLookup.getAliases(qt.entity)
          // (need to do it this way round to get the semantic links)
        if (null != masterAlias) {
          if (null == termBoolQ) {
            termBoolQ = QueryBuilders.boolQuery();
          }
          sQueryTerm.append(" OR ").append(sFieldName).append(":$manual_aliases");
          termBoolQ = termBoolQ.should(QueryBuilders.termQuery(sFieldName, qt.entity));
          termQ = termBoolQ = termBoolQ.should(QueryBuilders.termsQuery(sFieldName, masterAlias.getAlias().toArray()));

          // If want to add manual aliases as full text also...
          if ((null != qt.entityOpt) && qt.entityOpt.rawText) {
            if (null == this._extraFullTextTerms) {
              _extraFullTextTerms = new LinkedList<AdvancedQueryPojo.QueryTermPojo>();
            }
            String nonIndexField = null;
            if (AssociationPojo.docQuery_geo_index_ != sFieldName) { // (geo has no non-indexed form)
              nonIndexField = (AssociationPojo.docQuery_entity1_index_ == sFieldName) ? AssociationPojo.docQuery_entity1_ : AssociationPojo.docQuery_entity2_;
            }
           
            // (slightly annoying because I have to derive the dnames for all of them)
            for (String alias: masterAlias.getAlias()) {
              int nIndex2 = alias.lastIndexOf('/');
              String dName = alias.substring(0, nIndex2);
             
              if (EntityPojo.docQuery_index_ == sFieldName) { // (note: can use pointers here)
                AdvancedQueryPojo.QueryTermPojo qtExtra = new AdvancedQueryPojo.QueryTermPojo();
                qtExtra.etext = dName;
                _extraFullTextTerms.add(qtExtra);
              }
              else if (null != nonIndexField) {
                termQ = termBoolQ = termBoolQ.should(CrossVersionQueryBuilders.matchPhraseQuery(nonIndexField, dName));
              }
            }
            if (EntityPojo.docQuery_index_ == sFieldName) { // (note: can use pointers here)
              sQueryTerm.append(" OR (($manual_aliases").append("))");
            }
            else if (null != nonIndexField) {
              sQueryTerm.append(" OR ").append(nonIndexField).append(":$manual_aliases");                       
            }
           
          }//TESTED (entity + association) - aliases #1
         
          // Recall: we're abusing linkdata to contain aliases:
          if ((null != masterAlias.getSemanticLinks()) && !masterAlias.getSemanticLinks().isEmpty()) {
            String nonIndexField = null;
            if (AssociationPojo.docQuery_geo_index_ != sFieldName) { // (geo has no non-indexed form)
              nonIndexField = (AssociationPojo.docQuery_entity1_index_ == sFieldName) ? AssociationPojo.docQuery_entity1_ : AssociationPojo.docQuery_entity2_;
            }
           
            if (null == this._extraFullTextTerms) {
              _extraFullTextTerms = new LinkedList<AdvancedQueryPojo.QueryTermPojo>();
            }
            for (String textAlias: masterAlias.getSemanticLinks()) {
              if (EntityPojo.docQuery_index_ == sFieldName) { // (note: can use pointers here)
                AdvancedQueryPojo.QueryTermPojo qtExtra = new AdvancedQueryPojo.QueryTermPojo();
                qtExtra.etext = textAlias;
                _extraFullTextTerms.add(qtExtra);
              }
              else if (null != nonIndexField) {             
                termQ = termBoolQ = termBoolQ.should(CrossVersionQueryBuilders.matchPhraseQuery(nonIndexField, textAlias));
              }
            }
            if (EntityPojo.docQuery_index_ == sFieldName) { // (note: can use pointers here)
              sQueryTerm.append(" OR (($manual_text_aliases").append("))");
            }
View Full Code Here

   * OPTIONAL for all arg ontology_type (will apply a heuristic search only grabbing onts that level and below
   *
   */
  BaseQueryBuilder parseGeoTerm(AdvancedQueryPojo.QueryTermPojo.GeoTermPojo geo, StringBuffer sQueryTerm, GeoParseField parseFields)
  {
    BoolQueryBuilder boolQ = QueryBuilders.boolQuery().minimumNumberShouldMatch(1);
    List<String> ont_terms = null;
    //Get ontology types
    if ( null != geo.ontology_type )
    {     
      //get all ontology terms we are looking for
      ont_terms = GeoOntologyMapping.getOntologyList(geo.ontology_type)
    }
    else
    {
      ont_terms = GeoOntologyMapping.getOntologyList(null);       
    }
   
    if ((null != geo.centerll) && (null != geo.dist))
    {
      double lat, lon;
     
      if ('(' == geo.centerll.charAt(0)) {
        geo.centerll = geo.centerll.substring(1, geo.centerll.length() - 1);
      }
      String[] latlon = geo.centerll.split("\\s*,\\s*");
      if (2 == latlon.length)
      {
        lat = Double.parseDouble(latlon[0]);
        lon = Double.parseDouble(latlon[1]);
       
        char c = geo.dist.charAt(geo.dist.length() - 1);
        if ((c < 0x30) || (c > 0x39)) // not a digit, difference calculation is different
        {        
          //ENT
          //Add in ontology_type if necessary
          //in the end this results in query = CURR_GEO_QUERY AND (ONT_TYPE = [ont1 OR ont2 OR ont3])     
          if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.ENT )
          {           
            //use a 2nd variable so we dont have to keep casting termQ to BoolQuery
            BoolQueryBuilder subQ = QueryBuilders.boolQuery().must(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter(EntityPojo.docQuery_geotag_).distance(geo.dist).point(lat, lon)).boost(1.0F));
            subQ.must(QueryBuilders.termQuery(EntityPojo.docQuery_ontology_type_, ont_terms.toArray()))
            boolQ.should(QueryBuilders.nestedQuery(DocumentPojo.entities_, subQ).scoreMode("max").boost((float)1.0));
          }
         
          //ASSOC AND DOCGEO (only added if ont is point or null)
          if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.ASSOC )
            boolQ.should(QueryBuilders.nestedQuery(DocumentPojo.associations_, FilterBuilders.geoDistanceFilter(AssociationPojo.docQuery_geotag_).distance(geo.dist).point(lat, lon)).scoreMode("max").boost((float)1.0));
          if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.DOC )
            boolQ.should(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter(DocumentPojo.docGeo_).distance(geo.dist).point(lat, lon)));         
        }
        else // (identical to the above except geo distance parsing is different)
        {         
          //ENT
          //Add in ontology_type if necessary
          //in the end this results in query = CURR_GEO_QUERY AND (ONT_TYPE = [ont1 OR ont2 OR ont3]) 
          if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.ENT )
          {
            //use a 2nd variable so we dont have to keep casting termQ to BoolQuery
            BoolQueryBuilder subQ = QueryBuilders.boolQuery().must(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter(EntityPojo.docQuery_geotag_).distance(Double.parseDouble(geo.dist), DistanceUnit.KILOMETERS).point(lat, lon)).boost(1.0F));
            subQ.must(QueryBuilders.termsQuery(EntityPojo.docQuery_ontology_type_, ont_terms.toArray()));                         
            boolQ.should(QueryBuilders.nestedQuery(DocumentPojo.entities_, subQ).scoreMode("max").boost((float)1.0));
          }
          //ASSOC AND DOCGEO (only added if ont is point or null)
          if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.ASSOC )
            boolQ.should(QueryBuilders.nestedQuery(DocumentPojo.associations_, FilterBuilders.geoDistanceFilter(AssociationPojo.docQuery_geotag_).distance(Double.parseDouble(geo.dist), DistanceUnit.KILOMETERS).point(lat, lon)).scoreMode("max").boost((float)1.0));
          if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.DOC )
            boolQ.should(QueryBuilders.constantScoreQuery(FilterBuilders.geoDistanceFilter(DocumentPojo.docGeo_).distance(Double.parseDouble(geo.dist), DistanceUnit.KILOMETERS).point(lat, lon)));
        }
        sQueryTerm.append("dist(*.geotag, (").append(geo.centerll).append(")) < ").append(geo.dist);       
      }       
    }//TESTED logic11,logic12
    else if ((null != geo.minll) && (null != geo.maxll))
    {
      double latmin, lonmin, latmax, lonmax;
     
      if ('(' == geo.minll.charAt(0)) {
        geo.minll = geo.minll.substring(1, geo.minll.length() - 1);
      }
      String[] latlon1 = geo.minll.split("\\s*,\\s*");
      if ('(' == geo.maxll.charAt(0)) {
        geo.maxll = geo.maxll.substring(1, geo.maxll.length() - 1);
      }
      String[] latlon2 = geo.maxll.split("\\s*,\\s*");
     
      if  ((2 == latlon1.length) && (2 == latlon2.length))
      {
        latmin = Double.parseDouble(latlon1[0]);
        lonmin = Double.parseDouble(latlon1[1]);
        latmax = Double.parseDouble(latlon2[0]);
        lonmax = Double.parseDouble(latlon2[1]);
       
        // top left = max,min
        latmin = latmin < latmax ? latmin : latmax;
        latmax = latmin >= latmax ? latmin : latmax;
        lonmin = lonmin < lonmax ? lonmin : lonmax;
        lonmax = lonmin >= lonmax ? lonmin : lonmax;
                 
        // If we've got this far, we've found all the different locations       
        if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.ENT )
        {
          //use a 2nd variable so we dont have to keep casting termQ to BoolQuery
          BoolQueryBuilder subQ = QueryBuilders.boolQuery().must(QueryBuilders.constantScoreQuery(FilterBuilders.geoBoundingBoxFilter(EntityPojo.docQuery_geotag_).topLeft(latmax,lonmin).bottomRight(latmin, lonmax)).boost(1.0F));
          subQ.must(QueryBuilders.termsQuery(EntityPojo.docQuery_ontology_type_, ont_terms.toArray()));                         
          boolQ.should(QueryBuilders.nestedQuery(DocumentPojo.entities_, subQ).scoreMode("max").boost((float)1.0));
        }
       
        //ASSOC AND DOCGEO (only added if ont is point or null)     
        if ( parseFields == GeoParseField.ALL || parseFields == GeoParseField.ASSOC )
View Full Code Here

  // 1.2.3] Event term parsing - this one is pretty complex
 
  BaseQueryBuilder parseAssociationTerm(AdvancedQueryPojo.QueryTermPojo.AssociationTermPojo assoc, AdvancedQueryPojo.QueryTermPojo.SentimentModifierPojo sentiment, StringBuffer sQueryTerm )
  {
    boolean bFirstTerm = true;
    BoolQueryBuilder query = QueryBuilders.boolQuery()
    sQueryTerm.append("association:(");
   
    @SuppressWarnings("unused")
    int nTerms = 0; // (might be used later)
   
    if (null != assoc.entity1) {
      bFirstTerm = false;
      sQueryTerm.append("(");     
      this.parseAssociationSubTerm(assoc.entity1, sQueryTerm, query, AssociationPojo.docQuery_entity1_, AssociationPojo.docQuery_entity1_index_);
      sQueryTerm.append(')');
      nTerms++;
    }//TESTED
    if (null != assoc.entity2) {
      if (!bFirstTerm) {
        sQueryTerm.append(" AND ");       
      }
      bFirstTerm = false;
      sQueryTerm.append("(");
      this.parseAssociationSubTerm(assoc.entity2, sQueryTerm, query, AssociationPojo.docQuery_entity2_, AssociationPojo.docQuery_entity2_index_);
      sQueryTerm.append(')');
      nTerms++;
    }//TESTED
    if (null != assoc.verb) {
      if (!bFirstTerm) {
        sQueryTerm.append(" AND ");       
      }
      bFirstTerm = false;
      sQueryTerm.append("(verb,verb_category:").append(assoc.verb).append(")");
     
      query.must(QueryBuilders.boolQuery().should(QueryBuilders.queryString(assoc.verb).field(AssociationPojo.docQuery_verb_)).
                          should(QueryBuilders.queryString(assoc.verb).field(AssociationPojo.docQuery_verb_category_)));
     
      sQueryTerm.append(')');
      nTerms++;
    }//TESTED
    if (null != assoc.geo)
    {
      if (!bFirstTerm) {
        sQueryTerm.append(" AND ");       
      }
      bFirstTerm = false;
      sQueryTerm.append("(");
      query.must(this.parseGeoTerm(assoc.geo, sQueryTerm, GeoParseField.ASSOC));
      sQueryTerm.append(')');
      nTerms++;
    }//TOTEST
    if (null != assoc.time)
    {
      if (!bFirstTerm) {
        sQueryTerm.append(" AND ");       
      }
      bFirstTerm = false;
      sQueryTerm.append("(");
     
      // Top level: one of start OR end has to start inside the range (this is the first bit)
      // OR it must envelop the query
     
      // OK this one is a bit tricky because an event has a start+end time ... I think both
      // have to be inside the time range (fortunately because that's the easy case!)
      // (Note time_start and time_end don't exist inside the document object)
      StringBuffer sbDummy = new StringBuffer();
      BoolQueryBuilder combo2 = QueryBuilders.boolQuery();
      combo2.should(this.parseDateTerm(assoc.time, sQueryTerm, AssociationPojo.docQuery_time_start_, false));
      sQueryTerm.append(") OR/CONTAINS (");
      combo2.should(this.parseDateTerm(assoc.time, sQueryTerm, AssociationPojo.docQuery_time_end_, false));
      // (complex bit, start must be < and end must be >)
      BoolQueryBuilder combo3 = QueryBuilders.boolQuery();
      AdvancedQueryPojo.QueryTermPojo.TimeTermPojo event1 = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
      AdvancedQueryPojo.QueryTermPojo.TimeTermPojo event2 = new AdvancedQueryPojo.QueryTermPojo.TimeTermPojo();
      sQueryTerm.append("))");
      event1.min = "10010101";
      event1.max = assoc.time.min;
      event2.min = assoc.time.max;
      event2.max = "99990101"; // (ie the end of time, sort of!)
      combo3.must(this.parseDateTerm(event1, sbDummy, AssociationPojo.docQuery_time_start_, false)); // ie start time earlier than query
      combo3.must(this.parseDateTerm(event2, sbDummy, AssociationPojo.docQuery_time_end_, false)); // AND end time later than query
     
      query.must(combo2.should(combo3));
     
      query.must(combo2); // (this one is very simple - either one of the times must be inside the range)
      nTerms++;
View Full Code Here

TOP

Related Classes of org.elasticsearch.index.query.BoolQueryBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.