Lucene.Net.QueryParsers.QueryParser.Query C# (CSharp) Method

Query() public method

public Query ( String field ) : Query
field String
return Lucene.Net.Search.Query
        public Query Query(String field)
        {
            List<BooleanClause> clauses = new List<BooleanClause>();
            Query q, firstQuery = null;
            int conj, mods;
            mods = Modifiers();
            q = Clause(field);
            AddClause(clauses, CONJ_NONE, mods, q);
            if (mods == MOD_NONE)
                firstQuery = q;
            while (true)
            {
                switch ((jj_ntk == -1) ? Jj_ntk() : jj_ntk)
                {
                    case AndToken:
                    case OrToken:
                    case NotToken:
                    case PlusToken:
                    case MinusToken:
                    case LParanToken:
                    case StarToken:
                    case QuotedToken:
                    case TermToken:
                    case PrefixTermToken:
                    case WildTermToken:
                    case RangeInStartToken:
                    case RangeExStartToken:
                    case NumberToken:
                        break;
                    default:
                        jj_la1[4] = jj_gen;
                        goto label_1;
                }

                conj = Conjunction();
                mods = Modifiers();
                q = Clause(field);
                AddClause(clauses, conj, mods, q);
            }

        label_1:

            if (clauses.Count == 1 && firstQuery != null)
            {
                if (true) return firstQuery;
            }

            return GetBooleanQuery(clauses);
        }

Usage Example

Ejemplo n.º 1
0
    /// <summary>
    /// Gets the items from the GetIndexes()
    /// </summary>
    /// <param name="queryString">
    /// The query string.
    /// </param>
    /// <param name="useQueryParser">
    /// if set to <c>true</c> [use query parser].
    /// </param>
    /// <returns>
    /// Returns Query Result
    /// </returns>
    public QueryResult GetItems(string queryString, bool useQueryParser)
    {
      // Result object used to pass result and errormessages back to sender.
      QueryResult result = new QueryResult();

      List<string> indexes = this.GetIndexes();
      string[] resultItemIds = null;
      foreach (string indexName in indexes)
      {
        string database = "master";
        HighResTimer timer = new HighResTimer(true);

        // get the specified index
        Sitecore.Search.Index searchIndex = SearchManager.GetIndex(indexName);

        // get the database to perform the search in..
        Database db = Factory.GetDatabase(database);

        SearchHits hits;
        try
        {
          if (useQueryParser)
          {
            using (IndexSearchContext searchContext = searchIndex.CreateSearchContext())
            {
              // get a new standard analyser so we can create a query..
              Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
              QueryParser queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, queryString, analyzer);
              Query qry = queryParser.Query("_content");
              hits = searchContext.Search(qry, int.MaxValue);
            }
          }
          else
          {
            using (IndexSearchContext searchContext = searchIndex.CreateSearchContext())
            {
              // perform the search and get the results back as a Hits list..
              hits = searchContext.Search(queryString, int.MaxValue);
            }
          }

          result.Success = true;
          
          resultItemIds = new string[hits.Length];

          int i = 0;

          foreach (Document document in hits.Documents)
          {
            string str = document.Get("_docID");
            resultItemIds[i++] = str;
          }
        }
        catch (ParseException e)
        {
          result.ErrorMessage = e.Message;
          result.ParseException = e;
          result.Success = false;
        }
      }

      result.Result = resultItemIds;
      return result;
    }